[CMake] Using try_compile before project() command

Yuri Timenkov ytimenkov at parallels.com
Tue Jul 15 12:28:27 EDT 2008


On Tuesday 15 July 2008 20:01:52 Bill Hoffman wrote:
> Carminati Federico wrote:
> > Hello Yuri,
> >   I posted a similar question some time ago. There seems to be no way to
> > chose the compiler within the CMakeList.txt, because you can do very
> > little before the Project command, and afterwards it is too late.
> > Anybody can help here? best, regards,
>
> That is a very different question, and no, you can not change the
> compiler from within a CMakeLists.txt file.  It must be picked with CC
> or CXX environment variables before running CMake.
Yes, I just want to tune build environment but allow user to change it later. 
KDE, for example, overrides ones later by specifying COMPILE_FLAGS variables 
in CMake scripts without CACHE option. But such usage seems a bit tricky for 
me, because user doesn't know which flags will be used for build (or it's hard 
to determine place where particular flag came from, when you build with 
VERBOSE=1).

Overriding  CMake defaults seems most clear way, because user will see global 
settings in GUI and target-specific or source files-specific flags at the right 
place in CMakeLists.txt.

>
> > On 15 Jul 2008, at 15:56, Yuri Timenkov wrote:
> >> 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?
>
> Looks like a bug.   Please file a bug report with a small example that
> is easy to reproduce.
I'll try to figure out the problem.

>
> Thanks.
>
> -Bill

P.S. Did you think about "Best practices" category on wiki? Many people ask 
similar questions on list, and some of them are kind of "is it moral in 
CMake?" Because there is a lot of such questions, FAQ will be too long.



More information about the CMake mailing list