Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while loading shared libraries when running executable

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/lib$@.so $^
-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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading