如何应用不同的编译器选项为不同的编译器在cmake? [英] How to apply different compiler options for different compilers in cmake?
问题描述
我目前正在使用cmake来构建一些项目,主要平台是Visual C ++,MinGW GCC和Linux GCC。当使用GCC构建时,我需要指定 -Wno-invalid-offsetof
编译器选项。
如下:
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})
#为所有目标重复
这样工作,但假设除了visual studio之外的所有生成器都将使用gcc构建显然是不安全的。首先,Borland编译器有IIRC发生器。更重要的是,使用make不总是意味着使用gcc。
其他编译器我可能使用的是llvm-gcc和clang。幸运的是,我认为即使clang支持gcc兼容的选项。但是这个逻辑只有在相关代码永远不会被释放时才有效。
Cmake似乎检查可用的编译器,并为该编译器生成一个makefile问题 - 为什么不是至少可以选择直接构建项目,而不需要像make这样的中间人。)
在这种情况下,我是希望能够在我的CMakeLists.txt文件中直接测试gcc。到目前为止,我找不到一个合适的变量来测试或其他任何明显的解决方案。
这是否可能?
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)
>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
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.
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 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?).
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.
Is this possible?
解决方案To create a portable build system, it is best to not test for platforms, but to test for features.
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()
For C++ there is a similar CheckCXXCompilerFlag with a
check_cxx_compiler_flag(flag var)
command.这篇关于如何应用不同的编译器选项为不同的编译器在cmake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!