[CMake] CMAKE_C*_FLAGS undefined.

Philip Lowman philip at yhbt.com
Sat Apr 4 13:14:15 EDT 2009


On Sat, Apr 4, 2009 at 10:25 AM, Óscar Fuentes <ofv at wanadoo.es> wrote:

> Philip Lowman <philip at yhbt.com> writes:
> > 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?


Not sure exactly.  Hopefully someone else may have some insights here that
can help you.

One suggestion would be if you have non-definition flags you want to add to
every compilation you make, simply use CMAKE_C_FLAGS or CMAKE_CXX_FLAGS.

if(CMAKE_COMPILER_IS_GNU_CC)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,s -save-temps")
endif()

add_definitions() was originally intended for preprocessor definitions
only.  If you're using it to non-definition compilation flags I have no idea
how you'll be able to pull those out again.

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.


You can set CFLAGS and/or CXXFLAGS but they only work before the first
configure.  After that they are ignored.  If you need to change
CFLAGS/CXXFLAGS after that the best way is to modify CMAKE_C_FLAGS and
CMAKE_CXX_FLAGS in the CACHE.


> 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.


There might be a better way to do this I'm not aware of.  If so hopefully
someone else can chime in.
I believe the answer to your question (per directory in your source tree)
is:
   ${CMAKE_<lang>_FLAGS} + ${CMAKE_<lang>_FLAGS<build_type>} +
compile_definitions (at this directory).

This assumes you don't care about target properties like COMPILE_FLAGS or
DEFINE_SYMBOL.

add_definitions() is meant primarily as a way to add preprocessor
definitions.  In some projects it is often used in many directories
throughout source code and not always in the first CMakeLists.txt.  If all
of the preprocessor definitions you care about are defined in the toplevel,
however, CMakeLists.txt you should be able to use the COMPILE_DEFINITIONS()
property I mentioned.

-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090404/94f088ee/attachment.htm>


More information about the CMake mailing list