0

When I make the Makefile everything works fine, I get a library in the directory dir. And when I run "Make test" I get a testfile that I want to run. But when I want to run this file I get this weird error: ./programma: error while loading shared libraries: libprogramma.so: cannot open shared object file: No such file or directory. I have tried running the program on both WSL and Linux, but nothing makes this error go away. Can anyone help me?

Here I have my Makefile which makes the library and the executable:

INC_DIR     = include
SRC_DIR     = src
SOURCES     = $(sort $(shell find $(SRC_DIR) -name '*.cc'))
OBJECTS     = $(SOURCES:.cc=.o)
DEPS        = $(OBJECTS:.o=.d)
TARGET      = programma
CXX         = g++
CFLAGS      = -Wall -Wextra -Wpedantic -std=c++11
CPPFLAGS    = $(addprefix -I, $(INC_DIR))
.PHONY: all clean debug release
release: CFLAGS += -O3 -DNDEBUG
release: all
debug: CFLAGS += -O0 -DDEBUG -ggdb3
debug: all
all: $(TARGET)
clean:
    rm -f $(OBJECTS) $(DEPS) lib/*.so programma *.d
$(TARGET): $(OBJECTS)
    $(CXX) $(CFLAGS) $(CPPFLAGS) -fPIC -shared -o lib/[email protected] $^
-include $(DEPS)
%.o: %.cc
    $(CXX) $(CFLAGS) $(CPPFLAGS) -fPIC -MMD -o $@ -c $<
test:
    $(CXX) $(CFLAGS) -L./lib $(CPPFLAGS) -MMD -o programma tests/main.cc -l$(TARGET)

1 Answer 1

1

Executables on Linux don't look for shared libraries in the directory they're located in, at least by default.

You can either fix that at link-time, by passing -Wl,-rpath='$ORIGIN', or at runtime, by setting LD_LIBRARY_PATH env variable to the directory with the library. (LD_LIBRARY_PATH=path/to/lib ./programma)

Sign up to request clarification or add additional context in comments.

3 Comments

Can you maybe show me an example on how to fix it at link-time? With what I have to add to the makefile.
@Peredurz But I've told you the linker flag you need to add.
Oh, I have found what I need to do, thank you for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.