Vijay
Vijay

Reputation: 67211

dynamic linking:change of the linking path

Normally it happens that when ever the path of the library that has to be linked dynamically is defined in LD_LIBRARY_PATH or it it will be mentioned with -L flag while creating the binary.

In actual scenario if ,lets say the binary has been built and deployed at the client place. Now if there is a change in the path of one of the dynamic link library path. then we need to supply a new make file to all the clients where the binary was deployed.

is there any other method where we need not tell all the clients to change their makefiles and can something can be done in the code itself? if yes...could anybody please suggest how?

This was ironically an interview question that was asked to me and i didnot have the answer for it.

EDIT:: I was specifically asked about what can be done in the code without touching the makefile.

Upvotes: 0

Views: 2224

Answers (3)

qrdl
qrdl

Reputation: 34948

Use environment variable, like MYLIBPATH, and use this variable in your makefile, rather than hard-coded value.

So every client can have own directory structure, and as long as they correctly specify MYLIBPATH, your program builds ok.

Alternatively you can look for library in your makefile, like this

LIBPATH = $(shell find / -name libmylib.a -exec dirname {} ";" -quit)

myprog: myprog.c
        $(CC) myprog.c -lmylib -L$(LIBPATH)

EDIT: locate replaced with find that returns just first match with -quit option

Upvotes: 0

Francesco
Francesco

Reputation: 3250

Maybe the interviewers wanted to know about dlopen and dlsym? http://linux.die.net/man/3/dlsym

Upvotes: 0

YuppieNetworking
YuppieNetworking

Reputation: 8851

Usually you should only change the LD_LIBRARY_PATH, unless this might be related to a compilation with a hard-coded search path: rpath.

Upvotes: 1

Related Questions