G++ 链接器仅对共享库中的某些函数(不是全部)未定义引用

g++ linker undefined reference to only some functions (not all) from shared library

本文关键字:函数 全部 引用 未定义 链接 共享 G++      更新时间:2023-10-16

我正在尝试使用 g++ 链接到 Linux 中的第三方库我的 g++ 命令是

g++ -o "outdir/libmyLib.so.1.0" -Wl,--trace-symbol=_foo_bar_baz -Wl,--no-undefined -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -shared -Wl,--verbose obj_a.o obj_b.o foobarwrapper.o   -L/usr/local/lib/foobar `pkg-config --libs gtk+-3.0` -Wl,--start-group -llots -lof -ldeps -lfoobar -lz -ldl -lm -Wl,--end-group

这会从链接器生成未定义的引用

GNU ld (GNU Binutils for Ubuntu) 2.26.1
1>     Supported emulations:
1>      elf_x86_64
1>      elf32_x86_64
1>      elf_i386
1>      elf_iamcu
1>      i386linux
1>      elf_l1om
1>      elf_k1om
1>      i386pep
1>      i386pe
1>   using internal linker script:
1>   ==================================================
1>   /* Script for --shared -z combreloc -z now -z relro: shared library, combine & sort relocs */
1>   
1>   attempt to open /usr/local/lib/foobar/libfoobar.so succeeded
1>   /usr/local/lib/foobar/libfoobar.so: definition of _foo_bar_baz
1>   /obj/x64/Debug/foobarwrapper.o: In function `foobarwrapper::ThreadEntryRoutine()':
1>   /foobarwrapper.cpp:479: undefined reference to `_foo_bar_baz()'
1>   collect2: error: ld returned 1 exit status

我从这个库中总共使用了大约 30 个函数,但只有 4 个函数生成链接器错误。

我尝试将 -lfoobar 移动到不同的地方。它要么没有区别,要么所有库函数都无法链接。

我尝试根据类似问题的一些答案/评论将 -lfoobar 添加到编译命令中,但仍然没有爱。

我在这里做错了什么?

用extern "C" {}包装库的标头 #includes 似乎已经完成了这项工作。