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

Lucas.Pettey at engilitycorp.com Lucas.Pettey at engilitycorp.com
Thu May 28 13:32:42 EDT 2015


The CMAKE_*_COMPILER variables are set correctly.

The CMAKE_*_IMPLICIT_LINK_LIBRARIES have a whole bunch of libraries probably generated from the Cray wrapper.

It is interesting to note that the stdc++ library is in CMAKE_C_IMPLICIT_LINK_LIBRARIES and CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES but not in CMAKE_CXX_IMPLICIT_LINK_LIBRARIES.

Also, when the LINKER_LANGUAGE variable is not set, CMake links with the C compiler but does not add the stdc++ library.

On the Cray systems, the wrappers correctly invoke all necessary libraries, thus CMake is working harder than it needs to. Is there any way to tell CMake not to add these other IMPLICIT_LINK_LIBRARIES when doing  a mixed language build?

Thanks,
 
Lucas Pettey, PhD
HPCMP PETTT EQM CTA Lead
ERDC-DSRC OnSite
Vicksburg, MS
512-297-9833 Mobile (preferred)
601-634-2980 Office
lucas.pettey at engilitycorp.com

________________________________________
From: Brad King [brad.king at kitware.com]
Sent: Wednesday, May 27, 2015 12:36 PM
To: Pettey, Lucas @ EngilityCorp
Cc: cmake at cmake.org; Dan Liew
Subject: Re: [CMake] Linking C program to external C++ library

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