编译共享库,但其他链接器不工作
complie shared library ,but other linker doesn't work
我正在尝试编译从 SDL
开发的共享库,所以我正在使用 linkers flags
-lSDL2 -lSDL2_ttf -lm
但在测试时它没有 link
生成文件:
CC := gcc
CFLAGS := -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm
CTFLAGS := -Wall -lSDL2 -lSDL2_ttf -lm -L/home/mz37/programming/MZSDL
LDFLAGS = -shared
RM = rm -f # rm command
TARGET_LIB = libSGK.so # target lib
SRCS = src/mzsdl.c src/container.c
OBJS = $(SRCS:.c=.o)
all: ${TARGET_LIB}
test-container.out: ${TARGET_LIB} test/container-test.c
$(CC) $(CTFLAGS) -o $@ test/container-test.c -lSGK
$(TARGET_LIB): $(OBJS)
$(CC) ${LDFLAGS} -o $@ $^
$(SRCS:.c=.d):%.d:%.c
$(CC) $(CFLAGS) -MM $< >$@
include $(SRCS:.c=.d)
clean:
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d)
以及测试我的文件时的结果 test-container.out
:
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -MM src/container.c >src/container.d
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -MM src/mzsdl.c >src/mzsdl.d
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -c -o src/mzsdl.o src/mzsdl.c
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -c -o src/container.o src/container.c
gcc -shared -o libSGK.so src/mzsdl.o src/container.o
gcc -Wall -lSDL2 -lSDL2_ttf -lm -L/home/mz37/programming/MZSDL -o test-container.out test/container-test.c -lSGK
/usr/bin/ld: /tmp/ccm0XYCK.o: in function `main':
container-test.c:(.text+0x5b): undefined reference to `SDL_Init'
/usr/bin/ld: container-test.c:(.text+0x7a): undefined reference to `TTF_Init'
/usr/bin/ld: container-test.c:(.text+0xb2): undefined reference to `SDL_CreateWindow'
/usr/bin/ld: container-test.c:(.text+0xe9): undefined reference to `SDL_CreateRenderer'
/usr/bin/ld: container-test.c:(.text+0x139): undefined reference to `SDL_CreateRGBSurface'
.....
.....
/usr/bin/ld: /home/mz37/programming/MZSDL/libSGK.so: undefined reference to `SDL_GetError'
/usr/bin/ld: /home/mz37/programming/MZSDL/libSGK.so: undefined reference to `sqrt'
/usr/bin/ld: /home/mz37/programming/MZSDL/libSGK.so: undefined reference to `TTF_RenderText_Blended'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: test-container.out] Error 1
将 -o test-container.out test/container-test.c
放在 gcc -Wall
之后 之前 -l...
,顺序是相关的 linker需要知道丢失了哪些符号,它无法提前猜测(至少)container-test
需要什么
将 -lm
放在最后,您确定数学库不需要其他库,但可能其他库需要数学库。
请注意,在执行 link 时只需要 -lm
,而不是在编译时(选项 -c
)
我正在尝试编译从 SDL
开发的共享库,所以我正在使用 linkers flags
-lSDL2 -lSDL2_ttf -lm
但在测试时它没有 link
生成文件:
CC := gcc
CFLAGS := -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm
CTFLAGS := -Wall -lSDL2 -lSDL2_ttf -lm -L/home/mz37/programming/MZSDL
LDFLAGS = -shared
RM = rm -f # rm command
TARGET_LIB = libSGK.so # target lib
SRCS = src/mzsdl.c src/container.c
OBJS = $(SRCS:.c=.o)
all: ${TARGET_LIB}
test-container.out: ${TARGET_LIB} test/container-test.c
$(CC) $(CTFLAGS) -o $@ test/container-test.c -lSGK
$(TARGET_LIB): $(OBJS)
$(CC) ${LDFLAGS} -o $@ $^
$(SRCS:.c=.d):%.d:%.c
$(CC) $(CFLAGS) -MM $< >$@
include $(SRCS:.c=.d)
clean:
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d)
以及测试我的文件时的结果 test-container.out
:
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -MM src/container.c >src/container.d
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -MM src/mzsdl.c >src/mzsdl.d
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -c -o src/mzsdl.o src/mzsdl.c
gcc -Wall -Werror -fPIC -lSDL2 -lSDL2_ttf -lm -c -o src/container.o src/container.c
gcc -shared -o libSGK.so src/mzsdl.o src/container.o
gcc -Wall -lSDL2 -lSDL2_ttf -lm -L/home/mz37/programming/MZSDL -o test-container.out test/container-test.c -lSGK
/usr/bin/ld: /tmp/ccm0XYCK.o: in function `main':
container-test.c:(.text+0x5b): undefined reference to `SDL_Init'
/usr/bin/ld: container-test.c:(.text+0x7a): undefined reference to `TTF_Init'
/usr/bin/ld: container-test.c:(.text+0xb2): undefined reference to `SDL_CreateWindow'
/usr/bin/ld: container-test.c:(.text+0xe9): undefined reference to `SDL_CreateRenderer'
/usr/bin/ld: container-test.c:(.text+0x139): undefined reference to `SDL_CreateRGBSurface'
.....
.....
/usr/bin/ld: /home/mz37/programming/MZSDL/libSGK.so: undefined reference to `SDL_GetError'
/usr/bin/ld: /home/mz37/programming/MZSDL/libSGK.so: undefined reference to `sqrt'
/usr/bin/ld: /home/mz37/programming/MZSDL/libSGK.so: undefined reference to `TTF_RenderText_Blended'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: test-container.out] Error 1
将 -o test-container.out test/container-test.c
放在 gcc -Wall
之后 之前 -l...
,顺序是相关的 linker需要知道丢失了哪些符号,它无法提前猜测(至少)container-test
将 -lm
放在最后,您确定数学库不需要其他库,但可能其他库需要数学库。
请注意,在执行 link 时只需要 -lm
,而不是在编译时(选项 -c
)