当我这样做(动态链接)时,一切都很好:
$ gcc -o beta.bin ./game.c -lSDL -lSDLmain
但是当我尝试静态链接时:
$ gcc -static -o beta.bin ./game.c -lSDL -SDLmain
存在链接器问题:
/usr/local/bin/ld: /usr/local/lib/libSDL.a(SDL_aavideo.o): in function `AA_VideoInit':
SDL_aavideo.c:(.text+0x376): undefined reference to `aa_parseoptions'
/usr/local/bin/ld: SDL_aavideo.c:(.text+0x37b): undefined reference to `aa_defparams'
/usr/local/bin/ld: SDL_aavideo.c:(.text+0x380): undefined reference to `aa_autoinit'
#...more (log1.txt)
collect2: error: ld returned 1 exit status
我尝试显式指定静态库:
$ gcc -static -o beta.bin ./game.c /usr/local/lib/libSDL.a /usr/local/lib/libSDLmain.a /usr/lib/libm.a
黄道。没成功。
谢谢,神圣黑猫。 我尝试运行conf来找出必要的标志:
$ pkgconf --libs --static sdl
pkgconf 命令的结果:
-L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL -pthread -lSDL -pthread -lm -laa -lusbhid -pthread
但即使有这样一组标志,也会发生链接器错误。然而,错误是不同的。
$ gcc -static -o beta.bin ./game.c -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL -pthread -lSDL -pthread -lm -laa -lusbhid -pthread
gcc的结果
/usr/local/bin/ld: /usr/local/lib/libaa.a(aacurses.o): in function `curses_init':
aacurses.c:(.text+0x1f): undefined reference to `initscr'
/usr/local/bin/ld: aacurses.c:(.text+0x48): undefined reference to `termattrs'
/usr/local/bin/ld: aacurses.c:(.text+0x68): undefined reference to `stdscr'
/usr/local/bin/ld: aacurses.c:(.text+0x6f): undefined reference to `intrflush'
#...more (log2.txt)
所以我认为也有必要考虑 nсurses 标志。
$ pkgconf --libs --static ncurses
-fstack-protector-strong -Wl,-rpath,/usr/local/lib -lncurses -ltinfo
$ gcc -static -o beta.bin ./game.c -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL -pthread -lSDL -pthread -lm -laa -lusbhid -pthread -fstack-protector-strong -Wl,-rpath,/usr/local/lib -lncurses -ltinfo
而且看起来已经编译成功了。但事实并非如此。当我运行程序时,我在 ncurses 中得到输出:
$./beta.bin
Using AAlib driver: Curses driver 1.0 (curses)
[1;25r[m[4l[?1h=[?1000h[H[2J:
==========================================================================
[2;1H
==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==
[3;1H
====+=====+=====+=====+=====+=====+=====+=====+=====+=====+=====+=====+===
[4;1H
==+====+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+
[5;1H
=+==+=+=====+=====+=====+=====+=====+=====+=====+=====+=====+=====+=====+=
[6;1H
===+====+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===+=+===
[7;1H
==+===+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+=[8
#...more (log3.txt)
如果在没有 -static 标志的情况下编译,结果会很好。但由于某种原因, -static 标志给出了 ncurses 中的结果。 在此输入图片描述