I'm preparing a c++ app on linux (Ubuntu 16.04) with the use of a few poco libraries which I have dynamically linked. I have project folder that consists of : include, bin, lib , src and build folders and the relevant Makefile. So far I used the following Makefile which got the libraries from /usr/local/lib
CC := g++
# Folders
SRCDIR := src
BUILDDIR := build
TARGETDIR := bin
# Targets
EXECUTABLE := C++_APP
TARGET := $(TARGETDIR)/$(EXECUTABLE)
SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -c -Wall
INC := -I include -I /usr/local/include
LIB := -L /usr/local/lib -lPocoFoundation -lPocoNet -lPocoUtil
$(TARGET): $(OBJECTS)
@echo " Linking..."
@echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB)
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(BUILDDIR)
@echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
clean:
@echo " Cleaning...";
@echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)
.PHONY: clean
Now I'd like during running the linker to search for libraries only in project lib folder without changing LD_LIBRARY_PATH or editing ld.so.conf. So I searched and I found that this can be achieved by the linker argument -Wl,rpath,$ORIGIN. So I assume that I need to add the following statement
LDFLAGS := -Wl,-rpath,$ORIGIN/../lib
and change the the LIB statement as following:
LIB := -L $ORIGIN/../lib -lPocoFoundation -lPocoNet -lPocoUtil
However it still get the libraries from the default directory (usr/local/lib) , since I tested it with no library on the project lib folder. What have I done wrong?
No, you're misunderstanding. You need to pass the literal string $ORIGIN/../lib
as an argument to your linker. The $ORIGIN
token is kept inside your program after it's created and when the runtime linker starts to run your program it will replace $ORIGIN
with the current path that your program was invoked from. This is true even if you've copied your program somewhere else. So if you run your program as /usr/local/bin/myprogram
then the runtime linker will replace $ORIGIN
with /usr/local/bin
. If you copy it to /opt/mystuff/libexec/myprogram
then the runtime linker will replace $ORIGIN
with /opt/mystuff/libexec
.
In order to pass a literal $
to the command invoked by a make recipe, you have to escape the $
by doubling it: $$
. Otherwise, make will see the $
as introducing a make variable or function. Remember, it's perfectly legal for a make variable to avoid the parentheses etc., if it's a single character (note, $@
, $<
, etc.)
So when you write -Wl,-rpath,$ORIGIN/../lib
make will interpret the $O
in $ORIGIN
as expanding a variable named O
, which is empty, giving you -Wl,-rpath,RIGIN/../lib
.
Also you have to escape the $
from the shell, otherwise it will try to expand $ORIGIN
as a shell variable which you don't want.
You want to do something like this:
LDFLAGS = '-Wl,-rpath,$$ORIGIN/../lib' -L/usr/local/lib
LDLIBS = -lPocoFoundation -lPocoNet -lPocoUtil
$(TARGET): $(OBJECTS)
@echo " Linking..."
$(CC) $^ -o $@ $(LDFLAGS) $(LDLIBS)
(I don't know why you use @
to hide the command, then echo the command... why not just take out the @
and the echo
and let make show you the command?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With