How to apply different compiler options for differ

2019-02-01 13:32发布

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?

标签: cmake
1条回答
Lonely孤独者°
2楼-- · 2019-02-01 13:54

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.

查看更多
登录 后发表回答