'Undefined symbol when creating a shared object with SDL2
I'm trying to encapsulate some SDL2 functions into my own class. I'm working with C++ and compiling with g++.
Here's my error:
I'm creating a .so library with the following Makefile
NAME = arcade_sdl2.so
SRC = sdl2.cpp \
encapsulate/encapsulate.cpp
OBJ = $(SRC:.cpp=.o)
CFLAGS = -Wall -Wextra -Werror -fPIC
LIB_SDL2 = -lSDL2 -lSDL2_ttf -lSDL2_image -lSDL2_mixer
$(NAME): $(OBJ)
g++ -c $(CFLAGS) $(SRC) $(LIB_SDL2)
g++ -shared -o $(NAME) $(OBJ)
mv $(NAME) ../../lib/
all: $(NAME)
clean:
rm -f $(OBJ)
fclean: clean
rm -rf ../../lib/$(NAME)
re: fclean all
.PHONY: all clean fclean re
Then, in my class destructor, I need to call my member functions which are "MYSDL::SDL_DestroyWindow()" and "MYSDL::SDL_Quit()". To do so, I've simply returned the associated function from SDL2 lib:
void MYSDL::sdl_DestroyWindow(SDL_Window *target)
{
SDL_DestroyWindow(target);
}
and
void MYSDL::sdl_Quit(void)
{
SDL_Quit();
}
No error occurs during compilation, as my executable "arcade" is generated. But when i want to execute it in order to load my "arcade_sdl2.so" lib, I got the following error.
Cannot open library: lib/arcade_sdl2.so: undefined symbol: SDL_DestroyWindow
Does anyone has an idea about what to do now ?
Solution 1:[1]
From @HolyBlackCat,
I used the flags for SDL2 when i was compiling, but not linking.
I needed to transform my Makefile from :
$(NAME): $(OBJ)
g++ -c $(CFLAGS) $(SRC) $(LIB_SDL2)
g++ -shared -o $(NAME) $(OBJ)
mv $(NAME) ../../lib/
to
$(NAME): $(OBJ)
g++ -c $(CFLAGS) $(SRC)
g++ -shared -o $(NAME) $(OBJ) $(LIB_SDL2)
mv $(NAME) ../../lib/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
