[CMake] Re: compiling foo.c as a c++ file for visual studio projects

Jon W knowdat at gmail.com
Fri Jun 15 13:00:23 EDT 2007


On 6/15/07, Jon W <knowdat at gmail.com> wrote:
> I'm using cmake to generate solutions for Visual Studio 2003.  With
> several of the projects I need to compile the *.c files as c++ code.
>
> I'm trying to get the /TP flag to override the /TC flag that is
> automatically generated by cmake, but not having much luck.  I've read
> the mailing archive, and the only good workaround seemed to be
> renaming foo.c to FOO.C or foo.cpp.  The problem is that I have
> hundreds of files such as this from vendor libraries that I build, and
> it would be a continual task to rename files every time there is an
> update.
>
> Was the COMPILE_LANGUAGE property ever incorporated into cmake?
>   http://www.mail-archive.com/cmake@cmake.org/msg01326.html
>
> Any other solutions that people have gotten to work with Visual Studio?
>
> Thank you,
> Jon
>

I finally found a solution that works (of course just minutes after
the previous email).

------------------------------------------------
project( foo CXX )

set(foo_srcs
     foo.c
)

add_library( foo STATIC ${foo_srcs} )
set_target_properties ( foo PROPERTIES LINKER_LANGUAGE CXX )
------------------------------------------------

Note that the order of the add_library and set_target_properties is
critical, otherwise you will get an error.

fyi - Setting the /TP flag didn't seem to matter.  Neither did setting
CMAKE_C_COMPILER to ${CMAKE_CXX_COMPILER}.  Of course this applies
only generating visual studio projects.

-Jon


More information about the CMake mailing list