c++ - ld 报告缺少符号,但符号似乎存在

标签 c++ macos gcc linker undefined-symbol

我正在尝试将我的 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

(我稍微编辑了一下,删除了很长的目标文件列表)。

什么会导致此链接失败?该符号存在,并且是公开的,并且没有关于找不到库的错误或任何其他错误消息。

最佳答案

这些符号是未破坏的 C 符号。由于您已将其标记为 C++,因此我假设您正在使用 C++ 进行编译。如果这样做,您可能需要将库头文件包装在代码的外部 block 中:

extern "C" {
#include "library.h"
}

其中 library.h 是库头文件的名称,以防止它们在调用代码中被破坏。

关于c++ - ld 报告缺少符号,但符号似乎存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2543813/

相关文章:

c++ - 可以将容器的复制构造函数定义为不可复制值类型的删除吗?

macos - 如何在 Mac 版 Vim 版本 8 上启用 python

c++ - libpcap : how to get active network interface (Mac OSX)

c++ - 将基类添加到 "New C++ Class"对话框

c++ - 使用宏增加 C++ 代码的详细程度

swift - 杀死 :9 when running app with entitlements

c++ - 在 64 位机器上编译 32 位 :/usr/bin/ld: cannot find -l<someLibs>

c - GNU 中 _TRACE 宏在哪里定义?

gcc - aarch64 : NEON registers when compiling with gcc

c++ - 寻找 std::join 行为的说明