如何在 cmake 中为不同的编译器应用不同的编译器选项? [英] How to apply different compiler options for different compilers in cmake?

查看:86
本文介绍了如何在 cmake 中为不同的编译器应用不同的编译器选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 cmake 构建一些项目,主要平台是 Visual C++、MinGW GCC 和 Linux GCC.使用 GCC 构建时,我需要指定 -Wno-invalid-offsetof 编译器选项.

I'm currently working on using cmake to build some projects, with the main platforms being Visual C++, MinGW GCC and Linux GCC. When building with GCC, I need to specify the -Wno-invalid-offsetof compiler option.

我目前的修复如下...

My current fix is as follows...

if (   "${CMAKE_GENERATOR}" MATCHES "^Visual Studio"
    OR "${CMAKE_GENERATOR}" MATCHES "^NMake"
   )
    set (CPPLIB_COMPILER_OPTS "")
else ()
    set (CPPLIB_COMPILER_OPTS "-Wno-invalid-offsetof")
endif ()

...

set_target_properties(sh_core PROPERTIES COMPILE_FLAGS "${CPPLIB_COMPILER_OPTS}")
# repeated for all targets

这可行,但假设除visual studio 之外的所有生成器都使用gcc 构建显然是不安全的.首先,有用于 Borland 编译器的 IIRC 生成器.更重要的是,使用 make 并不总是意味着使用 gcc.

This works, but assuming that all generators other than the visual studio ones will build with gcc is obviously unsafe. For a start, there are IIRC generators for Borland compilers. More importantly, using make doesn't always mean using gcc.

我可能使用的其他编译器是 llvm-gcc 和 clang.幸运的是,我认为即使 clang 也支持与 gcc 兼容的选项.但这种逻辑只有在相关代码不发布的情况下才有用.

Other compilers I'm likely to use are llvm-gcc and clang. Fortunately, I think even clang supports gcc-compatible options. But this logic is only good for as long as the relevant code is never released.

Cmake 似乎会检查可用的编译器并专门为该编译器生成一个 makefile(提出了一个问题——为什么不至少可以选择直接构建项目,而不需要像 make 这样的中间人?).

Cmake appears to check for available compilers and generate a makefile specifically for that compiler (raising the question - why not at least have the option of building the project directly, without the need for a middle-man like make?).

既然如此,我希望能够在我的 CMakeLists.txt 文件中直接测试 gcc.但是,到目前为止,我找不到合适的变量来测试或任何其他明显的解决方案.

That being the case, I was hoping to be able to test directly for gcc in my CMakeLists.txt files. So far, though, I can't find an appropriate variable to test or any other obvious solution.

这可能吗?

推荐答案

要创建可移植的构建系统,最好不要测试平台,而是测试功能.

To create a portable build system, it is best to not test for platforms, but to test for features.

不要测试如果 Windows 执行此操作",而是测试如果 -Wno-invalid-offsetof 标志有效,则使用它".您可以使用 CheckCCompilerFlag 模块执行此操作,例如:

Instead of testing "if Windows then do this", test for "if the -Wno-invalid-offsetof flag works then use it". You can do that with the CheckCCompilerFlag module, for example:

include(CheckCCompilerFlag)
check_c_compiler_flag(-Wno-invalid-offsetof HAS_NO_INVALID_OFFSETOF)
if (HAS_NO_INVALID_OFFSETOF)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-invalid-offsetof")
endif()

对于 C++,有一个类似的 CheckCXXCompilerFlag 和一个 check_cxx_compiler_flag(flag var) 命令.

For C++ there is a similar CheckCXXCompilerFlag with a check_cxx_compiler_flag(flag var) command.

这篇关于如何在 cmake 中为不同的编译器应用不同的编译器选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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