Setting Path to Shared Library Inside a Makefile for Compile

adding shared library path to Makefile

The usual way is to copy the dynamic library during the default make and to one of the standard library path

/usr/local/bin

or one of your project library path and add the library to executable using

-L/project/specific/path

during make install.

Setting path to shared library inside a makefile for compile

Where I'm wrong ?

You are wrong in that you modified your Makefile incorrectly.

You have a macro Q, which evaluates to @, which makes the make quiet (not print the command it executes) if @ is the first character of the command line. By prepending LD_LIBRARY_PATH to the command line, you screwed that up:

# this is a quiet command:
@ar ...

# this is a not quiet command, which tries to execute @ar, which doesn't exist:
LD_LIBRARY_PATH=... ; @ar ...

The second part of wrong is that setting LD_LIBRARY_PATH as you did only affects the building of the libraries (i.e. the compiler and the linker). What you want is to affect the runtime using these libraries, not the compiler and linker used to build them.

As DevSolar correctly stated, you want -rpath instead:

$(Q)$(CC) $(SOFLAGS) -o $@ $(MINIZIP_DIR)/ioapi.so \
-Wl,-rpath=$(ZLIBDIR) $(MINIZIP_DIR)/zip.so ...

Setting path to shared library inside a makefile for execution

Each line in a recipe is run in its own shell, so change it to:

run:
export LD_LIBRARY_PATH=$(TESTLIB):$(DEPENDENCIES); \
./testit

or

run:
LD_LIBRARY_PATH=$(TESTLIB):$(DEPENDENCIES) ./testit

Change search path for shared library to rpath provided in Makefile

I have transferred libB.so.1.0 to my local lib-folder and linked the application against it using rpath. However, this path is not used to find libB.so.1.0 for libA.so(but the rpath is used for other shared-libraries that are linked directly).

That is the expected behavior when the application has RUNPATH (which is the newer default when -rpath is specified at link time).

You want the "recursive" behavior of RPATH instead. Documentation explaining the difference.

Step 1: confirm that your app is indeed using RUNPATH:

readelf -d a.out | egrep 'RPATH|RUNPATH'

Step 2: use -rpath=... -Wl,--disable-new-dtags to link your application, and confirm that RPATH is now being used.

Step 3: confirm that libB.so.1.0 is now found.

Enjoy :-)



Related Topics



Leave a reply



Submit