tags:

views:

76

answers:

3

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.

A: 

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

qrdl
Yeah..this was actually my answer...but this was not agreed by the panel.they wanted sth that can be done in the code
Vijay Sarathi
is the `locate` command present only on linux?
Vijay Sarathi
See updated answer - it uses POSIX `find` so it must be ok on all Unixes
qrdl
Using an environment variable is the correct answer. That the panel thought it should be done another way is not relevant here. We can't read their minds.
Hans Passant
+1  A: 

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

YuppieNetworking
A: 

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

Francesco
Yes...probably this is what they were expecting.thanks a lot for the right direction.
Vijay Sarathi