Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force cmake link against custom gcc libraries

Tags:

c++

linux

gcc

cmake

I'm trying to compile my program with custom gcc, after cmake generated makefiles. I've done following:

  1. Compiled gcc 5.2.0 from source
  2. Set cmake variable CMAKE_CXX_COMPILER to path to custom built, let's say /home/user/pkgs/bin/g++.
  3. Ran cmake, it completed successfully
  4. Ran make, it also completed succesfully

However, when I'm trying to run program it shows erros like: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version 'GLIBCXX_3.4.21' not found

It seems to me, that problem is in linking to old libraries in /usr/lib, whereas linking should be done to cutom gcc libraries.

How can I fix this?

like image 627
DoctorMoisha Avatar asked Oct 17 '15 10:10

DoctorMoisha


People also ask

How do I link a .so file in CMake?

Just do TARGET_LINK_LIBRARIES(program /path/to/library.so) > > > > 2. Better one is to: > > > > FIND_LIBRARY(MY_LIB NAMES library > > PATHS /path/to) > > TARGET_LINK_LIBRARIES(program ${MY_LIB}) > > > > Andy > > > > On Fri, 2004-10-29 at 10:19, Ning Cao wrote: > >> I want to link a .

What does CMake target_ link_ libraries do?

target_link_libraries is probably the most useful and confusing command in CMake. It takes a target ( another ) and adds a dependency if a target is given. If no target of that name ( one ) exists, then it adds a link to a library called one on your path (hence the name of the command).

Does CMake use GCC or G ++?

Usually under Linux, one uses CMake to generate a GNU make file which then uses gcc or g++ to compile the source file and to create the executable. A CMake project is composed of source files and of one or several CMakeLists.


1 Answers

The problem is not specific to CMake. You have it with all custom installs of GCC, that ship a new version of libstdc++.

You can either change your LD_LIBRARY_PATH to point to your gcc install path

export "LD_LIBRARY_PATH=/home/user/pkgs/lib:$LD_LIBRARY_PATH"

or you could link statically to libstdc++.so.6 by adding -static-libstdc++ to your CMAKE_CXX_FLAGS or you could change the rpath of your target to include /home/user/pkgs/lib (see cmake wiki on rpath) , however this only works if you run the program only on the machine it was compiled on.

like image 82
Finn Avatar answered Oct 28 '22 11:10

Finn