我有一个cpp程序,该程序使用SDL2中的SDL_TTF。我使用功能TTF_OpenFont
,TTF_RenderText_Solid
和TTF_CloseFont
。然后,我尝试将Makefile中的代码编译成一个库,以后可以使用dl_open
中的<dlfcn.h>
加载该库。
这是我的Makefike的样子
NAME = libSnakeSDL.so
SRC = SnakeSDL.cpp
SRC2 = SnakeSDL.cpp main.cpp
CC = LD_LIBRARY_PATH=$$PWD/SDL2/lib g++ -Wall -Wextra -Werror std=c++11
FLAGS = -L SDL2/lib -lSDL2 -lSDL2main -lSDL2_ttf -lfreetype
all: $(NAME)
$(NAME): fclean
@ # $(CC) -o snake $(SRC2) -I ../ -I SDL2/include $(FLAGS)
@ $(CC) -o $(NAME) -shared -fPIC $(SRC) -I ../ -I SDL2/include $(FLAGS)
clean:
@rm -fr $(NAME)
fclean : clean
re: fclean all
运行make
时出现此错误
/usr/bin/ld: SDL2/lib/libSDL2_ttf.a(SDL_ttf.c.o): relocation R_X86_64_PC32 against symbol `TTF_CloseFont' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Bad value collect2: error: ld returned 1 exit status Makefile:25: recipe for target 'libSnakeSDL.so' failed make: *** [libSnakeSDL.so] Error 1
深入研究了此错误的含义,发现了类似You can't use fPIC on a library that doesn't support fpic
的内容,但我对fpic和共享库还不熟悉。如何解决此问题/错误?