[CMake] How to make a shared library to use relative paths to the executable

Alexander Neundorf a.neundorf-work at gmx.net
Tue Oct 25 12:11:09 EDT 2011


On Tuesday 25 October 2011, Renato Utsch wrote:
> Hello!
> 
> I am learning to use CMake and I found it very easy to use, very and very
> good. But I don't understand why it uses full paths instead of relative
> paths when linking shared/dynamic libraries. I am currently making a
> program that is only compiled to a folder, but not installed to the system
> (on linux), much like a folder containing all the software in windows. It
> has the following structure (when compiled):
> 
> ---- program <-- Main folder of the project
> -------- bin <-- Executable and shared libraries of the executable
> -------- data <-- Stores data files and information for runtime
> -------- plugins <-- Stores shared libraries which (I want) can be added or
> removed after the compilation process which adds custom behaviour to the
> program.

As long as you don't "make install", they will all find each other, since by 
default cmake puts full RPATH into the ELF files.

> The real problem is: I can't link these shared libraries from the folder
> /program/plugins to the application at the /program/bin folder. I want this
> to be organized. How can I link by that?

Sorry, I don't understand what you mean.
 
> Other problem: the compilation has a lot of dependencies (qt, compiled
> boost libraries, opengl, between others), and this makes the compilation a
> bit difficult on Windows. So, I wanted to be able to zip the compiled
> folder (with all the needed libraries) and distribute the folder for the
> users. But when I move the program from one directory to another, it stops
> working. Is there any way, even hacky, to make CMake to use relative
> paths? Because all IDE's, other makes, allow doing this, so why CMake
> obligates us to use full paths? It would be so perfect by that...

If you want to create a zip file, you should use "make package" to do that.
During that step also "make install" will be executed.
In that step the install RPATH will be patched into the ELF files.
By default this will be empty (so the libs won't be found).
But you can set it explicitely using target properties.
If you want relative RPATH, you can use @ORIGIN for ELF files, e.g. for 
executables in bin you can set @ORIGIN/../lib/  (or just @ORIGIN as in your 
setup).

Alex


More information about the CMake mailing list