开发者

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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜