[CMake] CMAKE_C*_FLAGS undefined.

Óscar Fuentes ofv at wanadoo.es
Sat Apr 4 10:25:47 EDT 2009


Philip Lowman <philip at yhbt.com> writes:

>> > CMAKE_CXX_FLAGS / CMAKE_C_FLAGS is empty by default on Linux unless
>> you've
>> > modified it which may have been what happened.  The build type specific
>> > complilation flags are stored in CMAKE_CXX_FLAGS_<config_type>.
>> > CMAKE_CXX_FLAGS is a base that gets prepended to all of the build
>> solutions.
>> >
>> > The command pasted below works for me on CMake 2.6.4 RC3 Win32 using the
>> > MinGW generator (it outputs the flags used to compile during a Release
>> > build).  If something like this doesn't work for you can you reply with
>> your
>> > version of CMake and generator you're using?
>> >
>> > add_custom_target(foo COMMAND C:/cygwin/bin/echo.exe flags:
>> > ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE})
>>
>> CMAKE_CXX_FLAGS_RELEASE works, but it is not enough. It still misses the
>> definitions and other options which are passed to the
>> compiler. CMAKE_CXX_FLAGS, as noted on my previous message, is empty.
>>
>
>
>> add_custom_command(OUTPUT ${LLVM_CONFIG}
>>   COMMAND echo "flags: ${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CPP_FLAGS}
>> ${CMAKE_C_FLAGS}"
>>  COMMAND echo 's!@LLVM_CPPFLAGS@!${CMAKE_CPP_FLAGS}!' > temp.sed
>>  COMMAND echo 's!@LLVM_CFLAGS@!${CMAKE_C_FLAGS}!' >> temp.sed
>>  COMMAND echo 's!@LLVM_CXXFLAGS@!${CMAKE_CXX_FLAGS}!' >> temp.sed
>>  # TODO: Use general flags for linking! not just for shared libs:
>>  COMMAND echo 's!@LLVM_LDFLAGS@!${CMAKE_SHARED_LINKER_FLAGS}!' >> temp.sed
>>  COMMAND echo 's!@LIBS@!!' >> temp.sed                    # TODO: System
>> libs
>>  COMMAND echo 's!@LLVM_BUILDMODE@!${CMAKE_BUILD_TYPE}!' >> temp.sed
>>  COMMAND sed -f temp.sed < ${LLVM_CONFIG_IN} > ${LLVM_CONFIG}
>> # some stuff omitted
>>  DEPENDS ${FINAL_LIBDEPS} ${LLVM_CONFIG_IN}
>>  COMMENT "Building llvm-config script."
>>  )
>>
>> This builds an script that knows the compiler and linker options used
>> for building the libraries. Later, other (third party) software can
>> query the script and check those options for ensuring compatibility with
>> the libraries.
>>
>> `configure_file' is not okay because the "configuration" must be
>> executed at build time. CMAKE_CXX_FLAGS and co. are also empty at cmake
>> time though, so `configure_file' would have the same problem.
>
>
>
> You might want to have a look at the COMPILE_DEFINITIONS target property.

That's interesting, but its contents is sans the -D:

_GNU_SOURCE __STDC_LIMIT_MACROS __STDC_CONSTANT_MACROS

instead of

-D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS

This can be overcomed with string manipulation. But the docs says that
only flags beginning with -D or /D are added to this property. Where the
rest of the flags go?

add_definitions(-DFOO -Wl,s -save-temps)

will put FOO into the COMPILE_DEFINITIONS property, bug how can I access
the other flags?

> Another option of course is you can use configure_file() and

As explained on my previous message, configure_file is not adequate: the
file to be configured is generated at build time.

OTOH, what happens if the user sets CFLAGS and other environment
variables that affects the build after cmake is run? I ask just out of
curiosity, as I know that the method I'm using for configuring the file
does not take into account this possibility.

[snip]

> Can you attach a trivial reproducer of your problem?  I guess I'm hung
> up on what variables you're talking about that you think would change
> between CMake configure time and building time?  As far as I know the
> variables in your Makefile with the sole exception of make variables
> like VERBOSE=1 are all determined at CMake time.  configure_file()
> should make your life quite a bit easier I think.

Even if configure_file were adequate, the variables CMAKE_C_FLAGS
CMAKE_CXX_FLAGS etc are empty, when I expect them to have the full
compiler options for the current build type, including the stuff
introduced with add_definitions.

I can't believe that there is not a simple method for knowing such a
basic piece of information as "which compiler switches and options are
used for compiling source files of language X?" so it seems that I'm
missing something.

-- 
Oscar



More information about the CMake mailing list