[CMake] Trying to find the best way to compile C code to both C and C++ libraries

Jimi Damon jdamon at accesio.com
Sun Jul 14 16:49:13 EDT 2013


Hi,


I am new-ish to Cmake and hence might be overlooking a simple solution 
to this problem. Here's the quick crux of my problem.

I have a directory full of .C files that I want to compile to a C based 
Shared library and compile to a C++ shared library.  In the original 
Makefile, it was handled by a simple rule that did the following

g++ $(CXX_FLAGS)  file.c -o file.cpp.o

vs.

gcc $(C_FLAGS)  file.c -o file.o

What I tried doing in CMAKE was have two targets with one being C based 
library, and the other a C++ based. I then tried messing around with 
SET_SOURCE_FILES_PROPERTIES and SET_TARGET_PROPERTIES to change the 
compiler type for each set.

However, Cmake keeps track of the underlying files. So even if I had

file (GLOB c_files  RELATIVE "${CMAKE_SOURCE_DIR}" 
"${CMAKE_SOURCE_DIR}/*.c" )
followed by

file (GLOB cxx_files  RELATIVE "${CMAKE_SOURCE_DIR}" 
"${CMAKE_SOURCE_DIR}/*.c" )

ADD_LIBRARY( cxx_entries SHARED  cxx_files )
#and
ADD_LIBRARY( c_entries  SHARED c_files )


Then when I used the add_library () commands, it remembered only one 
compiler type for both, and hence either both libraries would be CXX 
compiled or C compiled.


So, my question is : what is the best way to handle one directory full 
of C files so that I can compile both to a C based shared library and  
C++ based shared library.

I thought about adding a custom rule, but considering that this will be 
migrated over to Windows , I am trying to rely on as much CMake as 
possible to try to ensure that it will work cross-platform.

Thanks for any suggestions,

-Jimi




More information about the CMake mailing list