[CMake] [CMAKE] Problem when moving an executable with a shared library

Michael Hertling mhertling at online.de
Wed Aug 24 08:28:42 EDT 2011


On 08/23/2011 06:11 PM, Renato Utsch wrote:
> Hello CMake Experts!
> 
> I don't know if it's here that I have to ask, so if I'm wrong, please tell
> me.
> 
> 
> I'm having a problem with CMake. Let me explain:
> 
> I use CMake to build a program and a shared library (let me say, "hello" and
> "libhello.so"). When I use them from my build folder (if the folder with the
> source is /tmp/hello, the folder with the build is /tmp/hello/build), they
> work perfectly. But if I move them to another folder (like to /home/Renato
> ), "hello" does not run, unless if i put the "libhello.so" in the folder
> /tmp/hello/build. I wanted to make the "hello" executable to look at the
> same folder to where it is, not to the "/tmp/hello/build" folder, which
> doesn't change.
> 
> How can I make that?

What do you mean exactly with "move them to another folder"? Do you
actually move the executable from the build tree to another location
on your own? That's a really bad idea; you should always use INSTALL()
commands for that purpose. One reason is the issue you have encountered:
The binaries in the build tree have references to this tree in order to
find accompanying shared libraries, and these references are usually not
valid at the binaries' final location. See the manpages of ld and ld.so
for more information about how and where shared libraries are searched.

If you definitely want to have ld.so look for shared libraries in the
directory of the executable about to start, you might set "\$ORIGIN"
as the executable's RPATH, refer to CMake's RPATH support. However,
$ORIGIN is used quite seldom and should be reserved for special
circumstances only.

'hope that helps.

Regards,

Michael


More information about the CMake mailing list