Skip to content

CMAKE_REQUIRED_LINK_OPTIONS breaks CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY

If setting CMAKE_REQUIRED_LINK_OPTIONS, to pass an option to cmake compile tests (similar to CMAKE_REQUIRED_FLAGS, but passing the option only to the linking command, not to compilation), this breaks when CMAKE_TRY_COMPILE_TARGET_TYPE is set to STATIC_LIBRARY.

When CMAKE_TRY_COMPILE_TARGET_TYPE is set to STATIC_LIBRARY, the options set in CMAKE_REQUIRED_LINK_OPTIONS are passed to the ar command too, while the options in CMAKE_REQUIRED_LINK_OPTIONS are expected to be C compiler linker driver commands.

An example:

include(CheckCXXCompilerFlag)
 
check_cxx_compiler_flag("-Wno-error" CXX_SUPPORTS_WNO_ERROR) # succeeds

list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "-L/some/odd/directory")
check_cxx_compiler_flag("-Wno-undef" CXX_SUPPORTS_WNO_UNDEF) # still succeeds
 
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
check_cxx_compiler_flag("-Wall" CXX_SUPPORTS_WALL) # fails

In this case, the last check fails (when both CMAKE_REQUIRED_LINK_OPTIONS is set and CMAKE_TRY_COMPILE_TARGET_TYPE is set to STATIC_LIBRARY).

CMakeFiles/CMakeError.log contains this:

Performing C++ SOURCE FILE Test CXX_SUPPORTS_WALL failed with the following output:
Change Dir: /home/martin/code/cmake-hello/build-make/CMakeFiles/CMakeTmp 
 
Run Build Command(s):/usr/bin/make cmTC_7777b/fast && /usr/bin/make -f CMakeFiles/cmTC_7777b.dir/build.make CMakeFiles/cmTC_7777b.dir/build 
make[1]: Entering directory '/home/martin/code/cmake-hello/build-make/CMakeFiles/CMakeTmp' 
Building CXX object CMakeFiles/cmTC_7777b.dir/src.cxx.o 
/usr/bin/c++    -DCXX_SUPPORTS_WALL   -Wall -o CMakeFiles/cmTC_7777b.dir/src.cxx.o -c /home/martin/code/cmake-hello/build-make/CMakeFiles/CMakeTmp/src.cxx 
Linking CXX static library libcmTC_7777b.a 
/usr/bin/cmake -P CMakeFiles/cmTC_7777b.dir/cmake_clean_target.cmake 
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7777b.dir/link.txt --verbose=1
/usr/bin/ar qc libcmTC_7777b.a -L/some/odd/directory CMakeFiles/cmTC_7777b.dir/src.cxx.o
/usr/bin/ar: invalid option -- 'L'
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information