I am trying to test the LD_LIBRARY_PATH
environment variable. I have a program test.c
as follows:
int main()
{
func("hello world");
}
I have two files func1.c and func2.c:
// func1.c
#include <stdio.h>
void func(const char * str)
{
printf("%s", str);
}
And
// func2.c
#include <stdio.h>
void func(const char * str)
{
printf("No print");
}
I want to do the following somehow:
- Convert
func1.c
andfunc2.c
to .so files – both with same namefunc.so
(they will be placed in different folders, saydir1
anddir2
- Compile
test.c
s.t. I only mention that it has a dependencyfunc.so
, but I don't tell it where it is (I want the environment variable to be used to find this) - Set the environment variable, in first try to
dir1
and in second try todir2
to observe different output in each run oftest
program
Is the above doable ? If so, how to do step 2 ?
I did the following for step 1 (same steps for func2):
$ gcc -fPIC -g -c func1.c
$ gcc -shared -fPIC -o func.so func1.o
Best Answer
Use
ld -soname
:-Wl,-soname,func.so
(this means-soname func.so
is passed told
) embedsSONAME
attribute offunc.so
in the output. You can examine it byreadelf -d
:Linked with this
func.so
withSONAME
,a.out
has that in itsNEEDED
attribute:Without
-Wl,-soname,func.so
, you'll get the following byreadelf -d
: