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

Brad Bell bradbell at seanet.com
Wed Jan 9 10:25:35 EST 2013


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 <kgardenia42 at googlemail.com> 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
>



More information about the CMake mailing list