2
votes

I'm trying to launch a client for a school project that has an AI developed in Lua, I have added liblua.so in a /lib/ folder at the root of my program's folder.

After compiling and launching said program, I get the following error:

./zappy_ai: error while loading shared libraries: liblua.so: cannot open shared object file: No such file or directory

From what I understand I must do something at the compilation for my program to know where my shared library is located.

Here's the relevant part of my Makefile:

CXX             =       g++

BASE_FLAGS      =       -Wall -Wextra -Iincludes

AI_NAME         =       zappy_ai

AI_PATH         =       ./sources/client/

AI_FLAGS        =       $(BASE_FLAGS)                   \
                        -L./lib/                        \
                        -I./includes/client/            \
                        -I./include/                    \
                        -std=c++11                      \

AI_LDFLAGS      =       -llua

AI_SRCS         =       main.cpp                        \
                        Client.cpp                      \
                        Params/Params.cpp               \
                        Params/Option.cpp               \
                        SocketTCP.cpp                   \
                        Misc/Error.cpp

AI_OBJS         =       $(addprefix $(AI_PATH), $(AI_SRCS:.cpp=.cpp.o))

%.cpp.o         :       %.cpp
                        @printf "%b[Compilation]%b %-50s" $(BLUE) $(RESET) $<
                        @$(CXX) $(FLAGS) -c $< -o $@
                        @printf "%bOK%b\n" $(GREEN) $(RESET)

$(AI_NAME)      :       FLAGS = $(AI_FLAGS)
$(AI_NAME)      :       $(AI_OBJS)
                        @$(CXX) $^ -o $@ $(AI_LDFLAGS)
                        @printf "%b[Message]%b AI compilation done\n\n" $(YELLOW) $(RESET)

What should I add in order to be able to launch my program and have it find my shared library?

1
You might need to run ldconfig or to add /usr/local/lib into /etc/ld.so.conf if you have compiled lua from source code - Basile Starynkevitch

1 Answers

1
votes

tldr:

$ LD_LIBRARY_PATH="$LD_LIBRARY_PATH:./lib" ./zappy_ai

Longer explanation:

You've dynamically linked

zappy_ai

against

./lib/liblua.so

by using

LDFLAGS = -L./lib/

and

LDLIBS = -llua

The resulting zappy_ai executable requires that same ./lib/ to be present within the LD_LIBRARY_PATH environment variable when the dynamic linker/loader attempts to resolve the -llua symbols that zappy_ai uses.