michael6866
michael6866

Reputation: 63

Link with relative paths in gcc

I will use a simplified example to explain my case.

I have two libme.so files which have different implementations and I already verified that they can work in a single design at the same time. One is located at ./root/v1/ and the other one is located at ./root/v2/

My "main" .so file is linked with those 2 libme.so files and is located at ./root/libtest.so

Now I have the requirement to make the things relocatable. That is, if I copy the entire "root" directory to another location or even another machine (assuming binary compatible), things should still work fine.

My question is, to make this work, what gcc command line should I use to build libtest.so?

I've tried the following two:

(1) (assuming I'm in "root" directory)

>gcc -shared -o libtest.so ./v1/libme.so ./v2/libme.so

This will make libtest.so has those 2 link dependencies both with absolute paths. This can be verified via ldd:

>ldd libtest.so
/home/design/root/v1/libme.so
/home/design/root/v2/libme.so

Obviously the paths are fixed. So once I relocate the "root" directory, it cannot find libme.so at runtime. Note setting LD_LIBRARY_PATH doesn't work in this case since the path from ldd is absolute path. The runtimer loader will not search LD_LIBRARY_PATH to find libme.so.

(2)

>gcc -shared -o libtest.so -lme -L./v1 -L./v2

This will work only if we have a single version of libme.so. In this case, the version in ./v2 will not get linked in. The same issue exists for -rpath.

Given that, what other options do I have?

Note there are some restrictions: (1) cannot rename libme.so to other names such as libme_v1.so (2) libtest.so has to link in both versions of libme.so

Upvotes: 5

Views: 4420

Answers (1)

michael6866
michael6866

Reputation: 63

Teppic is right. This turned out to be an issue only happening on one of my machines. Switching to another RHEL5.8 machine and I'm all set. It looks like the original machine installed a linker customized by someone.

Upvotes: 0

Related Questions