[cmake-developers] setting LINKER_LANGUAGE still adds -lstdc++

Brad King brad.king at kitware.com
Tue Oct 9 08:01:34 EDT 2012

On 10/09/2012 01:59 AM, James Bigler wrote:
> On Mon, Oct 8, 2012 at 10:40 PM, James Bigler <jamesbigler at gmail.com <mailto:jamesbigler at gmail.com>> wrote:
> In my project I need to manually link against a special version of libstdc++
>     add_library(a SHARED a.cpp)
>     set_target_properties(a PROPERTIES LINKER_LANGUAGE "C")
>     add_library(b SHARED b.c)
>     And here's the compiler output which has the -lstdc++ on the link line for library a, but not for library b.
> I told CMake to link the target as if it were a C target, then it decides that
> it needs CXX libraries because it found CXX sources in my library.  
> If I go through the trouble of specifying that I want to link the library as if
> it were C, why do the source files get to override my property?  

CMake is using the C compiler to drive the link call as you request.  That is
all the LINKER_LANGUAGE property says:

 "...sets the language whose compiler is used to link the target..."

However CMake knows that the sources have C++ code so it still wants to link against
the C++ runtime libraries.  This is the same feature that allows one to link C++ and
Fortran code together.  It chooses the C++ linker and adds the Fortran runtime libs.

It is a feature and it is working exactly as intended.  The C++ runtime libraries
are detected when the CXX language is enabled.  There is a CMakeCXXCompiler.cmake
file under the CMakeFiles directory of the top-level build directory.  It contains
settings like:


Whenever C++ sources are found in a target CMake will make sure these libraries
appear on the link line or are also implicit for the linker language.  You can
hide the C++ runtime libraries by erasing these values:


That along with setting LINKER_LANGUAGE to "C" should do what you want.


More information about the cmake-developers mailing list