[CMake] How to set platform-specific compiler-options from within CMakeFiles

Bill Hoffman bill.hoffman at kitware.com
Fri Jun 6 09:00:57 EDT 2008


Martin Apel wrote:
> Mike Jackson wrote:
>> typically, the PROJECT() statement is the first line of your 
>> CMakeLists.txt. Could you explain your project layout a bit more?
>>
>>
> I found out, that CMAKE_CXX_FLAGS is set to the compound of 
> $ENV{CXXFLAGS} and the platform-specific default at the time, the 
> PROJECT statement is executed.
> In order to preset CMAKE_CXX_FLAGS I tried to set $ENV{CXXFLAGS} from 
> within the CMakeFile. Thus this setting has to be done before the 
> PROJECT statement, otherwise
> CMAKE_CXX_FLAGS is already set. So I tried to include an external file by
> INCLUDE (${CMAKE_SYSTEM_NAME}.cmake)
> before the PROJECT statement. This doesn't work, because 
> ${CMAKE_SYSTEM_NAME} is not set at that time.
> 
> When setting CMAKE_CXX_FLAGS after the PROJECT statement, the 
> CMake-local variable is set correctly, but the cache is not updated. The 
> situation I tried to achieve, was to preset
> the cache variable CMAKE_CXX_FLAGS to include certain compiler flags. If 
> the user looked at the cache e.g. by using ccmake, he would also see and 
> be able to edit those additional flags.
> By setting the flags later, you only change the CMake-local variable, 
> but never the cache value, which is somewhat irritating, because the 
> compile happens with other flags than can be seen using
> ccmake.

You want to use this variable:
CMAKE_USER_MAKE_RULES_OVERRIDE

You can set that to a file that will be loaded at the right time so you 
can set the cache variables yourself.

-Bill


More information about the CMake mailing list