[CMake] CXX flags applied to C source

Michael Wild themiwi at gmail.com
Tue Jul 26 00:49:55 EDT 2011


Glad to hear.

I don't think that add_definitions() understands -U, so it won't
translate it for you, but pass it verbatim to the compiler. This means,
that -DFOO will become /DFOO for MSVC, you'll have to do the same
transformation yourself for -U. Nevertheless, personally I would choose
add_definitions(), as it IMHO is more semantically correct. The effect
is pretty much the same.

However, -U won't be able to undefine a #define'd symbol.

I was just asking about __x86__ and __linux__ because they are usually
defined by your compiler.

Michael

On 07/25/2011 11:19 PM, Kelly Burkhart wrote:
> Thanks, that seems to work.  I'm getting around to testing the windows
> version of this, we have a command line arg: "-UUNICODE" which
> explicitly removes a manifest constant.  I assume that should be in a
> set(...) rather than an add_definitions(...) correct?
> 
> The defines for __x86__ and __linux__ are required for the omnithread
> library which we use, and the gcc -W option is the old name for
> -Wextra.
> 
> Thanks,
> 
> -Kelly
> 
> On Mon, Jul 25, 2011 at 8:40 AM, Michael Wild <themiwi at gmail.com> wrote:
>> On 07/25/2011 03:20 PM, Kelly Burkhart wrote:
>>> Hi, I have a bunch of compiler flags specified with add_definitions as so:
>>>
>>> if(LINUX)
>>>   tb_compiler_version(TB_GCC_VERSION)
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-g")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-DBOOST_SIGNALS_NAMESPACE=tb_signals")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-D__x86__")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-D__linux__")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-D__OSVERSION__=2")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-D_REENTRANT")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-Wall")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-Wno-unused")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-Wno-comment")
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-Wno-sign-compare")
>>>
>>>   if("${TB_GCC_VERSION}" VERSION_GREATER "4.2")
>>>     add_definitions(${CMAKE_CXX_FLAGS} "-fno-strict-aliasing")
>>>   endif()
>>>
>>>   if("${TB_GCC_VERSION}" VERSION_GREATER "4.3")
>>>     add_definitions(${CMAKE_CXX_FLAGS} "-Wno-ignored-qualifiers")
>>>     add_definitions(${CMAKE_CXX_FLAGS} "-Wnon-virtual-dtor")
>>>   endif()
>>>
>>>   add_definitions(${CMAKE_CXX_FLAGS} "-W")
>>> else()
>>> ...
>>>
>>> The CXX flags are applied to C compiles which is mostly what I want,
>>> but there are some options (-Wnon-virtual-dtor for instance) that only
>>> apply to C++.  How can I specifiy an option should only be applied to
>>> C++ but not to C?
>>>
>>> Thanks,
>>>
>>> -Kelly
>>
>> For one, your are completely misusing add_definitions(). You should only
>> use it for -D flags, nothing else, and only if the definitions apply to
>> *all* files in that directory. If
>>
>> 1) the definitions should only be applied to some source files or some
>> targets, use the COMPILE_DEFINITIONS source/target property. See the
>> set_source_files_properties() and the set_target_properties() commands.
>>
>> 2) you want to set other compile flags, either append to the
>> CMAKE_CXX_FLAGS *variable* using the set() command, e.g.
>> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") if you want this flag to
>> be applied to everything that follows (including sub-directories), or
>> use the COMPILE_FLAGS source file or target property (see point 1).
>>
>>
>> Also, it doesn't make sense to pass the CMAKE_CXX_FLAGS variable to
>> add_definitions(). Lastly, you can pass definitions using a single call,
>> e.g. add_definitions(-DFOO -DBAR -DBAZ).
>>
>> So, your code might look something like the following:
>>
>> if(LINUX)
>>  tb_compiler_version(TB_GCC_VERSION)
>>  # TB_C_FLAGS will be also used for C++
>>  set(TB_C_FLAGS "-g -Wall -Wno-unused")
>>  set(TB_C_FLAGS "${TB_C_FLAGS} -Wno-comment -Wno-sign-compare")
>>  add_definitions(
>>    -DBOOST_SIGNALS_NAMESPACE=tb_signals    # Doesn't hurt in C files
>>    -D__x86__                               # ARE YOU SURE?!
>>    -D__linux__                             # ARE YOU SURE?!
>>    -D__OSVERSION__=2
>>    -D_REENTRANT
>>    )
>>
>>  if("${TB_GCC_VERSION}" VERSION_GREATER "4.2")
>>    set(TB_C_FLAGS "${TB_C_FLAGS} -fno-strict-aliasing")
>>  endif()
>>
>>  if("${TB_GCC_VERSION}" VERSION_GREATER "4.3")
>>    set(TB_C_FLAGS "${TB_C_FLAGS} -Wno-ignored-qualifiers")
>>    set(TB_CXX_FLAGS "${TB_CXX_FLAGS} -Wnon-virtual-dtor")
>>  endif()
>>
>>  # What's this?
>>  # add_definitions(${CMAKE_CXX_FLAGS} "-W")
>>
>>  # now, assign TB_<LANG>_FLAGS to CMAKE_<LANG>_FLAGS
>>  set(CMAKE_C_FLAGS "${TB_C_FLAGS}")
>>  set(CMAKE_CXX_FLAGS "${TB_C_FLAGS} ${TB_CXX_FLAGS}")
>> else()
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>>
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list