gpt4 book ai didi

c - "Undefined reference"共享库和 ldflags 错误

转载 作者:太空狗 更新时间:2023-10-29 15:40:55 25 4
gpt4 key购买 nike

当尝试使用 make 在 QEMU 中编译我的代码时,我在链接阶段遇到了一个undefined reference 错误。 Afaik 配置参数是正确的,因此应该正确使用共享库进行链接,但也许我忽略了一些东西。

如有任何帮助,我们将不胜感激!

下面是我已经收集到的大量附加信息:


解决方案:参见Zach's answer

我现在通过将 LIBS+=-lcity 添加到 Makefile.target 文件来管理它。

感谢大家!


更新:

使 V=1 输出:

cc -Werror -fPIE -DPIE -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
-D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef
-Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -I/home/user/cityhash
-Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs
-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers
-Wold-style-declaration -Wold-style-definition -Wtype-limits
-fstack-protector-all -I/usr/include/p11-kit-1 -I/usr/include/libpng12
-I/usr/include/pixman-1 -I../linux-headers -I..
-I/home/user/qemu/target-i386 -DNEED_CPU_H -I/home/user/qemu/include -pthread
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -O2
-D_FORTIFY_SOURCE=2 -g -Wl,--warn-common -Wl,-z,relro -Wl,-z,now -pie -m64 -g
-L/home/user/lib -Wl,-rpath=/home/user/lib -lcity -o qemu-system-x86_64 [… all
*.o files … ] ../libqemuutil.a ../libqemustub.a -lrt -pthread -lgthread-2.0
-lrt -lglib-2.0 -lutil -lrbd -lrados -lbluetooth -lcurl -lncurses -ltinfo
-lbrlapi -luuid -lpng12 -ljpeg -lsasl2 -lgnutls -lSDL -lX11 -lz -laio
-lpixman-1 -lm

接下来是我在下面编写的正常错误输出(LINK 除外,在本例中缺少它)。


错误:

~/qemu$ make
...
LINK x86_64-softmmu/qemu-system-x86_64
my_code.o: In function `function`:
/home/user/qemu/my_code.c:982: undefined reference to `CityHash64'
...
collect2: error: ld returned 1 exit status
make[1]: *** [qemu-system-x86_64] Error 1
make: *** [subdir-x86_64-softmmu] Error 2

配置参数:

./configure [...] \
--extra-cflags='-I/home/user/cityhash' \
--extra-ldflags='-L/home/user/lib -Wl,-rpath=/home/user/lib -lcity'

文件夹结构:

~
├─ cityhash/
│ ├─ city.h
│ └─ ...
├─ lib/
│ ├─ libcity.so
│ └─ ...
└─ qemu/

header 包含和函数使用(my_code.c):

[...]
11 #include "city.h"

982 hash = CityHash64(buf, len);
[...]

共享库的 Nm 输出:

~/lib$ nm libcity.so
[...]
0000000000000fdc T CityHash64
[...]

ma​​ke 期间的 Strace 输出:

~/qemu$ strace -f -o ../strace.out -- make
[...]
16111 stat("/home/user/lib/libcity.so", {st_mode=S_IFREG|0755, st_size=18468, ...}) = 0
16111 open("/home/user/lib/libcity.so", O_RDONLY) = 7
16111 fcntl(7, F_GETFD) = 0
16111 fcntl(7, F_SETFD, FD_CLOEXEC) = 0
16111 fstat(7, {st_mode=S_IFREG|0755, st_size=18468, ...}) = 0
16111 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2aaaaaad9000
16111 lseek(7, 0, SEEK_SET) = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 brk(0x2129000) = 0x2129000
16111 lseek(7, 12288, SEEK_SET) = 12288
16111 read(7, "9\23\2%\25\0\2\4\1\364\0\2\4\1\2E\23\0\2\4\1\2>\23\0\2\4\1\203\0\2\4"..., 4096) = 4096
16111 lseek(7, 16384, SEEK_SET) = 16384
16111 lseek(7, 16384, SEEK_SET) = 16384
16111 lseek(7, 0, SEEK_SET) = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 12288, SEEK_SET) = 12288
16111 read(7, "9\23\2%\25\0\2\4\1\364\0\2\4\1\2E\23\0\2\4\1\2>\23\0\2\4\1\203\0\2\4"..., 4096) = 4096
16111 lseek(7, 0, SEEK_SET) = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 8192, SEEK_SET) = 8192
16111 read(7, "\6\2\233\f\7\10\0\0$\0\0\0\314\1\0\0\314\354\377\377\210\0\0\0\0A\16\20\206\2C\r"..., 4096) = 4096
16111 lseek(7, 12288, SEEK_SET) = 12288
16111 lseek(7, 12288, SEEK_SET) = 12288
16111 lseek(7, 12288, SEEK_SET) = 12288
16111 lseek(7, 12288, SEEK_SET) = 12288
16111 read(7, "9\23\2%\25\0\2\4\1\364\0\2\4\1\2E\23\0\2\4\1\2>\23\0\2\4\1\203\0\2\4"..., 4096) = 4096
16111 lseek(7, 8192, SEEK_SET) = 8192
16111 read(7, "\6\2\233\f\7\10\0\0$\0\0\0\314\1\0\0\314\354\377\377\210\0\0\0\0A\16\20\206\2C\r"..., 4096) = 4096
16111 lseek(7, 0, SEEK_SET) = 0
16111 read(7, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\7\0\0\0\0\0\0"..., 4096) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
16111 lseek(7, 4096, SEEK_SET) = 4096
[...]
16111 close(7) = 0
[...]

最佳答案

好的,这似乎是一个链接排序问题。链接命令行的关键位是:

-g -L/home/user/lib -Wl,-rpath=/home/user/lib -lcity -o qemu-system-x86_64 

然后

[... all *.o files ...] ../libqemuutil.a ../libqemustub.a 
-lrt -pthread -lgthread2.0 [... more libraries ...]

问题是 -lcity 需要在命令行上所有 .o 文件之后,因为顺序很重要:库仅从目标文件中搜索未解析的符号,它们在命令行上之前。

这可能是 Automake 生成的 Makefile,这意味着您需要将 -lcity 放入 LDADD 以进行有问题的链接操作,而不是 LDFLAGS可能可以通过 --extra-libs=-lcity 配置选项(从 中删除 -lcity-- extra-ldflags,但保留所有其他内容)。如果没有,请尝试 make LDADD=-lcity。如果 that 不起作用(它有相当高的几率破坏其他需要在 LDADD 中的东西)你将不得不深入研究生成的 Makefile(从 Makefile.am 中看不出来),看看是否有另一个变量可以在命令行上设置。

关于c - "Undefined reference"共享库和 ldflags 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com