[CMake] How can I create a C executable and C++ library from the same source

Matthew Keeler mkeeler at tenable.com
Wed Jan 17 13:10:48 EST 2018


Whether what you want exactly is possible I dont know. If you are just
wanting your C++ test library to be able to test your C source then I would
still let them compile as C and just extern “C” { #include <your header> }.
When it gets linked all together it should be fine. This is how I usually
do testing of my C code with cpputest which is also a C++ library.

-- 
Matt Keeler


On January 17, 2018 at 13:05:36, Jimi Damon (jdamon at accesio.com) wrote:

Hi,

I want to use Gtest for some unit tests on a number of the C functions that
comprise my C executable.  However, Gtest ( And gMock ) are C++ testing
frameworks.

What I would like to have is a project for my exe and then a separate
project for my C++ library and be able to change the source properties to
use the language CXX just before compiling.

The problem I'm having is that CMake uses the CXX compiler for the C files.


Here's what I would like to have

project(cexe)
set(SOURCES foo.c bar.c )
add_executable( cexe  ${SOURCES} )

project(cpplib)
set_source_files_properties(${SOURCES} PROPERTIES LANGUAGE CXX )
add_library( cpplib SHARED ${SOURCES} )



Is there a way to do this ?

Can target_compile_options set the compile type for a given target ?


In the past I've had to copy the .C files to some renamed .CPP equivalent
and then compile off of that.

I'm hoping that enough new functionality has been added to Cmake to allow
separation between compiles.


Thanks,




--

Jimi Damon
ACCES I/O Products, Inc. <http://accesio.com/go.cgi?p=/contact/contact.html>
Linux Engineer
JDamon at accesio.com
(858) 550-7320 x3015
[image: ACCES I/O Logo]
10623 Roselle Street San Diego CA 92121-1506 <http://accesio.com/>


WARNING - This e-mail or its attachments may contain controlled technical
data or controlled technology within the definition of the International
Traffic in Arms Regulations (ITAR) or Export Administration Regulations
(EAR), and are subject to the export control laws of the U.S. Government.
Transfer of this data or technology by any means to a foreign person,
whether in the United States or abroad, without an export license or other
approval from the U.S. Government, is prohibited. The information contained
in this document is CONFIDENTIAL and property of ACCES I/O Products, Inc.
Any unauthorized review, use, disclosure or distribution is prohibited
without express written consent of ACCES I/O Products, Inc. If you are not
the intended recipient, please contact the sender and destroy all copies of
the original message and enclosed attachments. --

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180117/4c6fbc96/attachment.html>


More information about the CMake mailing list