[CMake] Compile project as C++ if MSVC

Michael Wild themiwi at gmail.com
Fri Dec 23 02:05:25 EST 2011


On 12/23/2011 07:23 AM, Eric Noulard wrote:
> 2011/12/23 Pau Garcia i Quiles <pgquiles at elpauer.org>:
>> Hi,
>>
>> Visual C++ 2010 does not support C99 yet and it seems it will be a
>> long time before MSVC supports it. For now, the usual work-around is
>> to build the project as C++.
>>
>> I'd like to build as C if using mingw, and as C++ if using MSVC. How
>> can I do that? project() seems not to be valid here (I can't do
>> if(MSVC) before project()) and I cannot find a target property to say
>> "compile as language CXX"
> 
> There is a LANGUAGE property but it is for source files not target
> (which is logical because a target may contain mixed language sources)
> 
> so
> 
> set_source_files_properties(<SRC> PROPERTIES LANGUAGE C)
> 
> see
> cmake --help-property LANGUAGE
> cmake --help-command set_source_files_properties
> 
> you may be interested by the SOURCE target property as well
> which makes its possible to retrieve all SOURCE file belonging to a
> target.
> 
> 

You could also fiddle around with the CMAKE_<LANG>_SOURCE_FILE_EXTENSION
variables. E.g:

if(MSVC)
  list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSION
    ${CMAKE_C_SOURCE_FILE_EXTENSION})
  set(CMAKE_C_SOURCE_FILE_EXTENSION)
endif()

Michael


More information about the CMake mailing list