ld报告缺少符号,但是这些符号似乎存在。

6

我想把我的Mac应用程序与美妙的libancillary库链接起来。但是,我已经更改了库构建脚本以创建共享库。我可以使用nm libancillary.dylib检查此库中的符号 - 结果为:

libancillary.dylib(single module):
         U ___sF
         U __keymgr_get_and_lock_processwide_ptr
         U __keymgr_get_and_lock_processwide_ptr_2
         U __keymgr_set_and_unlock_processwide_ptr
         U _abort
00002cfe T _ancil_recv_fd
00002c87 T _ancil_recv_fds
00002b6a T _ancil_recv_fds_with_buffer
00002e9e T _ancil_send_fd
00002e27 T _ancil_send_fds
00002d3f T _ancil_send_fds_with_buffer
         U _calloc
         U _dlopen
         U _dlsym
         U _fflush
         U _fprintf
         U _free
         U _malloc
         U _recvmsg
         U _sendmsg

然而,当我尝试链接我的应用程序时,输出结果如下:
g++ -headerpad_max_install_names -framework AppKit -framework Cocoa -framework IOKit -framework CoreFoundation -framework Carbon -framework OpenGL -framework SystemConfiguration -framework Security -Wl,-bind_at_load -arch i386 -o MyApp build/app.o build/client.o build/util.o -F/Library/Frameworks -L/Library/Frameworks -L../ancillary -lancillary
Undefined symbols:
  "ancil_recv_fd(int, int*)", referenced from:
      CIPCUnixUtils::readFD(int, int&) constin utils.o
  "ancil_send_fd(int, int)", referenced from:
      CIPCUnixUtils::writeFD(int, int) constin utils.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [ABClient] Error 1

这种链接失败的原因是什么?符号存在且为公开状态,没有关于找不到库或其他错误信息的错误提示。

(我稍微编辑了一下,删除了非常长的对象文件列表。)


类似的问题在这里:http://stackoverflow.com/questions/942754/nm-reports-symbol-is-defined-but-ldd-reports-symbol-is-undefined除了我的符号被显示为公共的。 - Thomi
你不需要使用 nm -D 命令来查看动态链接符号吗?或者在OSX上,与Linux不同的是其工作方式吗? - misiu_mp
2个回答

7
那些符号是未经处理的C语言符号。由于您将其标记为C ++,我假设您正在使用C ++进行编译。如果您这样做,您可能需要在代码中使用extern块包装库的头文件:
extern "C" {
#include "library.h"
}

library.h是库文件的头文件名,为了防止它们在调用代码中变形而命名成这个。


哦,我应该想到的。谢谢。 - Thomi

1

我想知道这是否是C++的名称重载问题?

尝试在utils.o文件上运行nm命令,并查看它实际要查找的符号。

你可能需要在头文件中加入extern C来解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接