CMake `link_directories` for libstdc++.so

1.8k views Asked by At

I am using CMake's link_libraries code to specify a path to (the folder containing) a particular version of libstdc++.so.

Initially, on a 32-bit machine, this worked fine: running ldd <target> on a built-but-not-installed executable showed that libstdc++.so was pointing to the desired version, and I was able to run ctest to execute tests even though the system libstdc++.so is older than what the tests require.

When I ported the code to a 64-bit machine, however, the desired libstdc++.so path was no longer included in the linked executables. This forces me to (ab)use LD_LIBRARY_PATH to run the project's tests.

I tried to replicate the issue with a small test-case, but I can't get it to include the desired paths on either platform, so it's possible that the issue is not related to the 32-bit to 64-bit porting activity. I have, however, commented out the link_libraries line when building on the 32-bit platform and confirmed that without it, the desired library path is not included. I have also checked many times to ensure that I am specifying a valid path for the desired 64-bit library.

Any idea what else might be going on here?

1

There are 1 answers

5
mistapink On

You might want to read about the RPATH option: http://en.wikipedia.org/wiki/Rpath

The basic search order is:[1]

The (colon-separated) paths in the DT_RPATH dynamic section attribute of the binary if present and DT_RUNPATH attribute does not exist. The (colon-separated) paths in the environment variable LD_LIBRARY_PATH, unless the executable is a setuid/setgid binary, in which case it is ignored.

You can of course set it manually via CMAKE_[EXE,SHARED,STATIC,MODULE]LINKER__FLAGS e.g.

cmake -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-rpath -Wl,/path/to/lib"

but there is also a possibility to use CMake itself http://www.cmake.org/Wiki/CMake_RPATH_handling