[CMake] Linking C program to external C++ library

Brad King brad.king at kitware.com
Wed May 27 13:36:42 EDT 2015


On 05/21/2015 06:00 PM, Lucas.Pettey at engilitycorp.com wrote:
> I am trying to compile a code written in C, but using an external
> library that contains C++ functions. I am on a Cray system and
> everything works if I manually link with the cray CC wrapper.
> When I change the linker language with this command:
> 
> set_property(TARGET adh PROPERTY LINKER_LANGUAGE CXX)
> 
> CMake is using the CC wrapper, but adding -lstdc++ to the link line.
> This causes an error. Can anyone tell me how to not get this added library?

CMake detects the libraries implicitly added by the compiler front-end
for each language.  When mixing languages it chooses to link using the
compiler front-end for one of the languages and adds the implicit
libraries needed for other languages and not added by the chosen one.
This case looks strange because you are telling it to link with the
C++ compiler but CMake is still adding the implicit library for C++.

Look at CMakeFiles/${CMAKE_VERSION}/CMake*Compiler.cmake for details
of the compiler detected for each language.  Look for these variables:

 CMAKE_C_COMPILER
 CMAKE_C_IMPLICIT_LINK_LIBRARIES

 CMAKE_CXX_COMPILER
 CMAKE_CXX_IMPLICIT_LINK_LIBRARIES

To what are they set?

Thanks,
-Brad


More information about the CMake mailing list