2010-03-30 111 views
6

我試圖將我的mac應用程序鏈接到美妙的libancillary庫。但是,我更改了庫構建腳本以創建共享庫。我可以用nm libancillary.dylib檢查這個庫中的符號 - 其結果是:ld報告丟失的符號,但符號似乎存在

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 

(我已編輯這個稍微刪除很長的目標文件列表)。

什麼可能導致此連接失敗?該符號存在並且是公共的,並且不能找到該庫或任何其他錯誤消息。

+0

類似的問題在這裏: http://stackoverflow.com/questions/942754/nm-reports-symbol-is-defined-but-ldd-reports-symbol-is-undefined 除了我的符號顯示作爲公衆。 – Thomi 2010-03-30 08:38:10

+0

與linux相比,您不必使用nm -D來查看動態鏈接的符號,還是它在osx上的工作方式不同? – 2012-03-21 16:42:47

回答

7

這些符號是未加密的C符號。正如你已經將它標記爲C++,我假設你正在用C++進行編譯。如果你這樣做,你可能需要包裝的庫的頭在你的代碼在extern塊文件:

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

其中library.h是庫的頭文件(S),以防止它們在被錯位的名字調用代碼。

+0

Doh--我應該想到這一點。謝謝。 – Thomi 2010-03-30 08:52:47

1

我不知道這是一個C++的名稱 - mangling問題嗎?

嘗試在utils.o文件上運行nm,並查看它實際正在查找的符號。

您可能必須將標頭包裝在extern C中。