[CMake] Building a library for both C and C++

kgardenia42 kgardenia42 at googlemail.com
Wed Jan 9 12:53:23 EST 2013


On Wed, Jan 9, 2013 at 8:57 AM, Nils Gladitz <gladitz at sci-vis.de> wrote:
> Why do you want to build a C library both with a C++ and a C compiler?
> Wouldn't a C compiler build and an extern "C" interface suffice to use the
> library with both C and C++?

I didn't fully qualify my use-case.

Let's say I have source files:  foo.c, foo.cpp and bar.c

I would like to build:

* a C library (mylib.a) containing foo.c and bar.c
* a C++ library (mylib++.a) containing foo.cpp and bar.c

At the moment I have a CMake define:

option(WITH_CXX "Build the C++ library" OFF)

Which is used to set the SRC files in CMakeLists.txt (foo.c vs
foo.cpp) as appropriate.

This leads to the current workflow of:

   # build my C library
   mkdir build_c
   cmake ..
   make

   # build my C++ library
   mkdir build_cpp
   cmake -DWITH_CXX=ON ..
   make

What I would *like* to do is build both in one go without the -DWITH_CXX stuff.

Is this a cmake-ish thing to do?  Can anyone point me to any examples
of libraries that do this?

One problem I encountered while doing this is that the use of a C++
compiler for source files seems to be global if I use:

SET_SOURCE_FILES_PROPERTIES( ${mylib_SRCS} PROPERTIES LANGUAGE CXX )

Whereas I want to have one target which builds the C++ library,
another which builds the C library and then both of them for "all" to
require both of them.  I guess maybe your point is that for the C++
library there is no need to build the C files with a C++ compiler as
long as I build the C++ files with a C++ compiler (as long as all the
extern stuff is done correctly).  That seems a reasonable point but
I'm still not sure on the overall work-flow of how to build 2
libraries with one "make" invocation.

I'd greatly appreciate a sketch of what this might look like or a
pointer to an Open Source project which does something like this as I
have spent a while struggling with it without much success.  I suspect
I am thinking about it wrongly somehow.

Thanks.






>
> In case you want to provide an additional distinct C++ specific interface
> I'd assume you'd have actual C++ source files which you could use for the
> C++ target and which could link to the C library.
>
> Nils
>
>
> On 01/09/2013 01:17 AM, kgardenia42 wrote:
>>
>> Hi list,
>>
>> I am trying to write a CMakeLists.txt where the same source files are
>> compiled separately to both a C library and a C++ library.
>>
>> So, lets say I have foo.c, bar.c and baz.c and I would like to build
>> mylib.a and mylib++.a
>>
>> My intuition is to use one CMakefile and add multiple targets for each
>> library I want to build.  Does that seem like a reasonable approach?
>> Other suggestions welcome.  Also, can anyone point me to an open
>> source library that uses cmake and builds both a C and C++ version.
>>
>> If that approach (one CMakeLists.txt to build both libraries) seems
>> reasonable then how can I override the language per target?
>>
>> I know that I can force the use of a C++ compiler for source files:
>>
>> SET_SOURCE_FILES_PROPERTIES( ${mylib_SRCS} PROPERTIES LANGUAGE CXX )
>>
>> However, this seems to set the language globally and so now all
>> targets use the C++ compiler.
>>
>> I couldn't seem to figure out how to do the equivalent thing
>> per-target.  Any suggestions?  i.e. is there another way (other than
>> SET_SOURCE_FILES_PROPERTIES) which could accomplish the "set the
>> language per target" functionality I need here?
>>
>> Thanks.
>> --
>>
>> 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
>
>
>
> --
> Nils Gladitz, B.Sc.
> DICOM, Konnektivität und Entwicklung
>
> Scivis wissenschaftliche Bildverarbeitung GmbH
> Bertha-von-Suttner-Str. 5
> D-37085 Göttingen
> GERMANY
> Handelsregister Nr. / Trade Register No. B3100 Göttingen
> Geschäftsführer / Managing Directors Dr. Gernot Ebel, Dr. Uwe Engeland
>
> Tel: 0049 (0)551 634181-28
> E-Mail: gladitz at scivis.de
> Web: www.scivis.de
>


More information about the CMake mailing list