Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Don't set rpath for a single library used in link

Tags:

linux

gcc

cmake

ld

What I'd like to do is configure my CMakeLists file so that while building my project the linker uses a copy of a shared library (.so) that resides in my build tree to link the executable against but then does not set the rpath in the linked executable so that the system must provide the library when the loader requests it.

Specifically, I want to link against libOpenCL.so during build time on a build farm that doesn't have libOpenCL.so installed as a system library. To do this, libOpenCL.so is in the project build tree and referenced using an absolute path in the CMakeLists file. This absolute path is to ensure that if the system does happen to have libOpenCL.so installed then it is not used.

However, when running the final executable, CMake has added the absolute path to the rpath which stops the system version of libOpenCL.so being picked up by the library loader and used.

Seems simple but I can't quite figure it out.

Thanks!

like image 907
user2746401 Avatar asked Mar 03 '16 15:03

user2746401


People also ask

What is rpath or rpath link?

In computing, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries. Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library).

What is $origin in rpath?

$ORIGIN is a special variable that indicate actual executable filename. It is resolved to where the executable is at run-time, and can be quite useful when setting RPATH.

How are shared libraries loaded?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

What is Chrpath?

chrpath changes, lists or removes the rpath or runpath setting in a binary. The rpath, or runpath if it is present, is where the runtime linker should look for the libraries needed for a program.


1 Answers

I know this answer is super late. I faced the same requirement as yours. Either we need is whitelist approach where we set CMAKE_BUILD_RPATH explicitly with what we need. Or we need a blacklist approach where we tell cmake, which RPATHs we don't want in the executable. Way to remove RPath from build tree is not documented yet: https://gitlab.kitware.com/cmake/cmake/issues/16825

The solution I took is:

Set RUNPATH instead of RPATH. You can achieve this by the statement:

SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags")

When RUNPATH is present, RPATH is ignored. RUNPATH - same as RPATH, but searched after LD_LIBRARY_PATH, supported only on most recent UNIX

Then I can achieve the overriding the library using the environment variable LD_LIBRARY_PATH.

like image 175
sushantsha Avatar answered Oct 24 '22 07:10

sushantsha