使用SCons TryCompile检查Windows上的编译器标志支持 [英] Using SCons TryCompile to examine compiler flag support on Windows

查看:133
本文介绍了使用SCons TryCompile检查Windows上的编译器标志支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助GCC和clang,我已经能够使用SCons的 TryCompile功能来构建简单的配置检查,以确定当前配置的编译器是否支持给定的编译标志。基本上,克隆环境,将有问题的标志添加到CFLAGS,CCFLAGS或CXXFLAGS(视情况而定),执行TryCompile,如果TryCompile成功,则支持该标志,我们可以将其添加到实际的环境中。

With GCC and clang, I've been able to use SCons 'TryCompile' feature to build a simple configure check to determine if the currently configured compiler supports a given compile flag. Basically, clone the env, add the flag in question to CFLAGS, CCFLAGS, or CXXFLAGS, as appropriate, execute TryCompile, and if the TryCompile succeeds, then the flag is supported and we can add it to the real env.


  • 这与gcc完美配合,因为未知标志是错误,编译器以非零状态退出。

  • 使用clang时,它的工作原理也相当不错:clang默认情况下会将未知错误视为警告,但是如果将它传递给-Werror,它将把未知标志变成错误。因此,如果它知道我们正在使用clang,则围绕TryCompile的包装程序始终会传递-Werror和要测试的标志。

但是,这一切都落在了Microsoft工具链上,因为据我所知,没有办法说服编译器将未知标志视为错误:即使您将标志传递给,它们也始终是警告。发出警告错误。由于编译会干净地退出,并且是否接受该标志,因此TryCompile总是成功。参见这个问题,详细说明了我为使MSVC退出非零状态所做的各种尝试。

However, this all falls over with the Microsoft toolchain because as far as I can discover, there is no way to convince the compiler to treat unknown flags as errors: they are always warnings, even if you pass the flag to make warnings errors. Since the compile exits cleanly wither or not the flag is accepted, TryCompile always succeeds. See this question for details on the various attempts I have made to get MSVC to exit with a non-zero status.

关于我该如何进行这项工作?我是否还有另一个SCons设施可以为我完成这项工作?我应该在MS平台上插入TryCompile并分析编译器输出,而不是检查退出状态。我真的很高兴使用TryCompile来配置clang和gcc的时间标记检测,但是如果我无法让MSVC进行合作,我将需要放弃整个方法,并且我很不愿意这样做

Any ideas on how I can make this work? Is there another SCons facility that I'm overlooking that can do this job for me? Should I interpose on TryCompile on MS platforms and parse the compiler output rather than examining the exit status. I'm really happy with using TryCompile for configure time flag detection with clang and gcc, but if I can't get MSVC to cooperate I'm going to need to abandon this whole approach, and I'm pretty loathe to do that since it is working so well so far.

推荐答案

让它再次出现在游行队伍中:)显然,Windows编译器不管发生什么,总是返回成功。

Leave it to Windows to rain on the parade once again :) Obviously the Windows compiler always returns success, irregardless of what happens.

我可以想到几种可以尝试的选择。

I can think of several options that you could try.

首先,SCons提供了多平台配置(Autoconf功能),可以帮助您达到相同的结果。它不包含任何用于编译器选项的内容,但至少包含以下内容:

First of all, SCons provides a Multi-Platform Configuration (Autoconf Functionality) which may help you achieve the same result. It doesnt include anything for compiler options, but does at least includes the following:


  • 检查头文件的存在

  • 检查功能的可用性

  • 检查库的可用性

  • 检查typedef的可用性

  • 添加您自己的自定义支票

  • Checking for the Existence of Header Files
  • Checking for the Availability of a Function
  • Checking for the Availability of a Library
  • Checking for the Availability of a typedef
  • Adding Your Own Custom Checks

另一种选择是使用以下方法构建某种词典: Microsoft编译选项。每个编译器版本可能需要一个字典。此特定选项可能需要花费很长时间来准备,并且可能不值得。

Another option would be to build some sort of a dictionary with the Microsoft compilation options. You would probably need one dictionary per compiler version. This particular option would probably take a long time to prepare, and probably wouldnt be worth it.

另一种选择是改用Object()或Program()生成器TryCompile()构建器,然后尝试 catch 失败并做出相应反应。我不确定SCons是否允许您将编译失败作为异常捕获并在失败时继续执行,但是值得检查。

Another option would be to use the Object() or Program() builder instead of the TryCompile() builder, and try to catch the failure and react accordingly. Im not sure if SCons allows you to catch compilation failures as an exception and carry on if it fails, but its worth checking into.

这篇关于使用SCons TryCompile检查Windows上的编译器标志支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆