Building a library for both C and C++

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Building a library for both C and C++

kgardenia42
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
Reply | Threaded
Open this post in threaded view
|

Re: Building a library for both C and C++

Nils Gladitz
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++?

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: [hidden email]
Web: www.scivis.de

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Building a library for both C and C++

Andreas Pakulat-2
In reply to this post by kgardenia42
Hi,

On Wed, Jan 9, 2013 at 1:17 AM, kgardenia42 <[hidden email]> wrote:

> 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.

I'd say this does not make any sense. The mylib++.a will still be a C
library because all the compiled code in it will be C code unless your
c files are somehow using a preprocessor macro to enable C and C++
code. And if they I'd say you'd be better off to split the files into
a .c and a .cpp one and drop the macro.

A C++ program can link to C libraries just fine, so there's really no
need for that that I can see right now.

Andreas
--

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
Reply | Threaded
Open this post in threaded view
|

Re: Building a library for both C and C++

Brad Bell
I have the same problem. The reason for the problem is that I am
comparing the speed of the exact same source code compiled by C and C++.
I have heard folk lore that C is faster, so I want an automated test to
check for this for an arbitrary hardware and compiler.

What I did was to create two source files, copied from the exact same
original file, but with the .c and .cpp extensions using CONFIGURE_FILE. See
https://projects.coin-or.org/CppAD/browser/trunk/compare_c/CMakeLists.txt


On 01/09/2013 02:27 AM, Andreas Pakulat wrote:

> Hi,
>
> On Wed, Jan 9, 2013 at 1:17 AM, kgardenia42 <[hidden email]> wrote:
>> 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.
> I'd say this does not make any sense. The mylib++.a will still be a C
> library because all the compiled code in it will be C code unless your
> c files are somehow using a preprocessor macro to enable C and C++
> code. And if they I'd say you'd be better off to split the files into
> a .c and a .cpp one and drop the macro.
>
> A C++ program can link to C libraries just fine, so there's really no
> need for that that I can see right now.
>
> Andreas
>

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Building a library for both C and C++

Eric Wing
In reply to this post by Andreas Pakulat-2
>
> On Wed, Jan 9, 2013 at 1:17 AM, kgardenia42 <[hidden email]>

>> So, lets say I have foo.c, bar.c and baz.c and I would like to build
>> mylib.a and mylib++.a
>>

I concur with the others that usually this is unnecessary and simply
building a C library with extern "C" guards for C++ is usually
sufficient.

Though as somebody who has been burned by C++ way too many times, I am
aware of nasty cases where compiling as C or C++ does make a
difference. Two examples off the top of my head:
- set/long jump vs. exception handling: mixing the two is a bad idea.
I actually have a really esoteric case of this in a patch I wrote for
Lua to enable Objective-C exception handling which adds another layer
of complexity because there are interactions with C++ exception
handling if you want to compile in support for it. But usually you
have #ifdefs for this kind of stuff in your code.
- function pointers: Function pointers are not guaranteed to work
across C and C++ calling conventions. Some platforms have a different
calling convention for C++ and if you try passing/using a C++ function
pointer to a function that is extern "C", it may not work correctly.

But most people don't worry about these things and writing a C library
is the most portable/compatible thing you can do. C++ users are used
to self-inflicted wounds so making them handle any subtle
incompatibility repercussions is the norm.

Anyhow, if you really need to build two libraries, I don't necessarily
have a good solution for you, but one idea is to use CMake to copy
your .c file into a .cpp file on the fly and add it to a second
separate C++ library target.

-Eric
--
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/
--

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
Reply | Threaded
Open this post in threaded view
|

Re: Building a library for both C and C++

kgardenia42
In reply to this post by Nils Gladitz
On Wed, Jan 9, 2013 at 8:57 AM, Nils Gladitz <[hidden email]> 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: [hidden email]
> Web: www.scivis.de
>
--

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
Reply | Threaded
Open this post in threaded view
|

Re: Building a library for both C and C++

Matthew Woehlke-2
On 2013-01-09 12:53, kgardenia42 wrote:

> On Wed, Jan 9, 2013 at 8:57 AM, Nils Gladitz <[hidden email]> 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

Maybe I am confused... if bar.c does not contain C++ source code, what
is the problem building bar.c with the C compiler and linking in into
your C++ library?

IOW (overly simplified example), I would expect this to work:

add_library(my_c_lib foo.c bar.c)
add_library(my_cpp_lib foo.cpp bar.c)


If you really have to, you could get clever and do something like:

# replace B with your build dir, e.g. CMAKE_CURRENT_BINARY_DIR
add_custom_target(
   OUTPUT ${B}/bar.cpp
   DEPENDS bar.c
   COMMAND ${CMAKE_COMMAND} -E copy ${B}/bar.cpp bar.c
)
add_library(my_cpp_lib foo.cpp ${B}/bar.cpp)

--
Matthew

--

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