[CMake] Using try_compile before project() command

Yuri Timenkov ytimenkov at parallels.com
Tue Jul 15 09:56:59 EDT 2008


I want to tune initial compile and linker flags to put into cache.
in main CMakeLists.txt:

# Override default Compiler flags
set(CMAKE_USER_MAKE_RULES_OVERRIDE MyCompilerFlags)
project(MyProject)

in MyCompilerFlags.cmake:

check_cxx_linker_flag("-Wl,--warn-unresolved-symbols" HAVE_LD_WARN_UNRESOLVED_SYMBOLS)

if(HAVE_LD_WARN_UNRESOLVED_SYMBOLS)
	set(CMAKE_SHARED_LINKER_FLAGS_INIT "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--warn-unresolved-symbols")
endif(HAVE_LD_WARN_UNRESOLVED_SYMBOLS)

This works fine for CMake 2.4:
-- Performing Test -Wl,--warn-unresolved-symbols
-- Performing Test -Wl,--warn-unresolved-symbols - Success
-- Performing Test -Wl,--no-undefined
-- Performing Test -Wl,--no-undefined - Success
-- Check for working C compiler: /usr/bin/gcc32
-- Check for working C compiler: /usr/bin/gcc32 -- works
...

But for CMake 2.6 this code fails with error:
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Performing Test -Wl,--warn-unresolved-symbols
CMake Error: Unknown extension ".cxx" for file "/vz/build/test/CMakeFiles/CMakeTmp/src.cxx".  TRY_COMPILE only works for enabled languages.
Currently enabled languages are: C CXX
See PROJECT command for help enabling other languages.
-- Performing Test -Wl,--warn-unresolved-symbols - Failed
-- Performing Test -Wl,--no-undefined
CMake Error: Unknown extension ".cxx" for file "/vz/build/test/CMakeFiles/CMakeTmp/src.cxx".  TRY_COMPILE only works for enabled languages.
Currently enabled languages are: C CXX
See PROJECT command for help enabling other languages.
-- Performing Test -Wl,--no-undefined - Failed
-- Check for working C compiler: /usr/bin/gcc32
-- Check for working C compiler: /usr/bin/gcc32 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
...

where check_cxx_linker_flag macro looks line check_cxx_compiler_flag:

file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
	"int main() { return 0;}\n")
message(STATUS "Performing Test ${_FLAG}")
try_compile(${_RESULT}
	${CMAKE_BINARY_DIR}
	"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
	CMAKE_FLAGS -DCMAKE_EXE_LINKER_FLAGS="${_FLAG}"
	OUTPUT_VARIABLE OUTPUT
	)

Any suggestions?


More information about the CMake mailing list