Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding shared library path to Makefile

Tags:

makefile

I want to add the shared library path to my Makefile. I have put in the export command in the makefile, it even gets called, but I still have to manually export it again. What is the correct approach?

Makefile:

SOURCES = kwest_main.c fusefunc.c dbfuse.c logging.c dbbasic.c dbinit.c dbkey.c metadata_extract.c plugins_extraction.c import.c

LIBS = -L$(LIB) -lfuse -lsqlite3 -lkw_taglib -ltag_c -ltag -Wl,-rpath=.

INCLUDE = ../include
LIB = ../lib

EXE = kwest

CC = gcc

CCFLAGS = -g -Wall -Wextra -std=gnu99 -pedantic-errors -I$(INCLUDE)

OFLAGS = -c

ARCH = $(shell getconf LONG_BIT)

X = -D_FILE_OFFSET_BITS=$(ARCH)

OBJECTS = $(SOURCES:.c=.o)

$(EXE) : $(OBJECTS)
    $(CC) -o $(EXE) $(OBJECTS) $(LIBS)

%.o: %.c
    $(CC) $(OFLAGS) $(CCFLAGS) $< 

fusefunc.o: fusefunc.c
    $(CC) $(OFLAGS) $(CCFLAGS) $< $X

kwest_libs: kw_taglib
--->export LD_LIBRARY_PATH=$(LIB):$LD_LIBRARY_PATH

kw_taglib: plugin_taglib

plugin_taglib: plugin_taglib.o kwt_upd_meta.o
    gcc -g -shared -I$(INCLUDE) -Wl,-soname,libkw_taglib.so -o $(LIB)/libkw_taglib.so -ltag -ltag_c plugin_taglib.o kwt_upd_meta.o

plugin_taglib.o:
    gcc -c -g -I$(INCLUDE) -Wall -Wextra -pedantic-errors -std=gnu99 -fPIC -ltag_c -c plugin_taglib.c

kwt_upd_meta.o:
    g++ -c -g -I$(INCLUDE) -Wall -Wextra -pedantic-errors -fPIC -ltag kwt_upd_meta.cpp

c: clean

clean:
    rm -rf *.o
    rm -rf *.db

ca: cleanall

cleanall: clean
    rm -rf $(EXE)

ob: cleanall
    rm -rf ~/.config/$(EXE)/

Execution:

$ ./kwest mnt
./kwest: error while loading shared libraries: libkw_taglib.so: cannot open shared object file: No such file or directory
$ export LD_LIBRARY_PATH=../lib:D_LIBRARY_PATH
$ ./kwest mnt
"executes correctly"
like image 751
coolharsh55 Avatar asked Feb 27 '13 16:02

coolharsh55


2 Answers

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.

like image 97
Pradheep Avatar answered Sep 19 '22 00:09

Pradheep


As already mentioned here, the thing you probably want is the linker option -rpath.

Like that, you can set a default search path for the binary. Looks like you even already use -rpath in your makefile, but you specify the wrong path:

LIBS = -L$(LIB) -lfuse -lsqlite3 -lkw_taglib -ltag_c -ltag -Wl,-rpath=.

So the binary will search in the current directory for dyn-libraries. However, you add ../lib to your LD_LIBRARY_PATH later, for execution of the binary, so the given path . seems to be wrong.

Please take a try for the following fix:

LIBS = -L$(LIB) -lfuse -lsqlite3 -lkw_taglib -ltag_c -ltag -Wl,-rpath=../lib

Like that you should not need to specify a LD_LIBRARY_PATH for execution.

like image 38
Alex Avatar answered Sep 20 '22 00:09

Alex