[CMake] Building CLR project OR removing COMPILE_FLAGS

Neil MacMullen neil.macmullen at neul.com
Thu Apr 7 10:24:48 EDT 2011


Thanks Michael,

>Usually, CMake has no idea that a ".c" file like src/main.c is to be
>treated by a C++ compiler, so it applies the CMAKE_C_FLAGS[_<CONFIG>]
>variables to the compilation. Thus, you probably need to tweak these
>variables to suit your needs instead of the C++ ones. Moreover, note
>that the non-configuration-specific CMAKE_<LANG>_FLAGS variables are
>in effect for any configuration. As an alternative, you might change
>the language associated with src/main.c to C++ by setting the file's
>LANGUAGE source property accordingly. 

After a lot of experimentation I'd managed to come to the conclusion that CMAKE_C_FLAGS was what to tweak although your explanation of _why_ is most helpful !  

Unfortunately this still isn't the whole solution; somewhere, the compile option /TC (interpret as 'C' is being set which is overriding my own attempt to set /TP (interpret as C++).  I've tried dumping every _FLAGS  variable using the message function to see if I can find out where this is creeping in but without success.   I've grepped the cmake share directory but can find no trace of it there.  I have a horrible feeling it is baked into the executable; the cmake source shows it as an option in cmVS10CLFlagTable.h. 

Your suggestion of manually changing the language is an excellent one though and is sufficient to provide a working build. For the benefit on anyone else who may be searching in future, here's the entire working file.

  cmake_minimum_required (VERSION 2.6)
  project (test )
  add_subdirectory("src")
  set_source_files_properties(src/main.c    PROPERTIES LANGUAGE "CXX")
  STRING(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  STRING(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clr /FUotherstuff.dll")
  add_executable(test src/main.c)


>Two further recommendations:
>- When setting source properties, always use full paths as it isn't
>  documented if relative paths refer to the source directory or the
>  build tree. In this regard, keep generated source files in mind.
>- If there's nothing CMake can do for you in a directory, just do
>  not enter it, i.e. no ADD_SUBDIRECTORY() command, instead of
>  placing an empty CMakeLists.txt file therein.

Great advice - thanks again,

  Neil








More information about the CMake mailing list