[CMake] test_compile with compiler flags

Dustyn Blasig dustyn at blasig.us
Fri Jul 12 13:11:03 EDT 2019


Hi All,

I'm sure there is a way to do this, but I'm not seeing it. I need to test a
compiler flag to know whether to enable it for our entire build. So I had
planned on using test_compile(), but I don't see a way to pass additional
compiler flags to test_compile() through CMAKE_FLAGS and there's no option
to pass environment variables like CXXFLAGS with test_compile().

Also, when do the variables like CMAKE_CXX_FLAGS get set? I enable the CXX
language and specify a required support for C++11, but if I dump
CMAKE_CXX_FLAGS right after that there are no options such as -std=c++11 in
the variable. I thought I could do something like ..

try_compile(.. CMAKE_FLAGS -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} <extra
args>" ..)

... but CMAKE_CXX_FLAGS is empty as described above so I get errors about
the compiler not supported C++11 features since -std=c++11 wasn't set.

Is there an example of testing a compiler flag I could try?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190712/24086ef0/attachment.html>


More information about the CMake mailing list