2012-04-17 152 views
0

我有一個非常簡單的問題,我一直試圖找出最近6個小時左右。我只想在Mac OS X上構建一個動態庫,然後使用該庫構建一個應用程序。我創建了.dylib並編譯了測試應用程序,但是當我運行該應用程序時,我得到:如何在Mac OS X上構建和使用動態庫?

Joes-Mac-Pro:Desktop Joe $ ./test dyld:Library not loaded:./lib /simple_library.dylib 從引用:/Users/Joe/Desktop/./test 原因:沒有找到圖片 跟蹤/ BPT陷阱:5

我試圖在可執行文件的目錄做一個lib文件夾,並把裏面dylib,同樣的錯誤。我試着將dylib放入可執行路徑本身,同樣的錯誤。我試過使用install_name_tool來更改可執行文件中dylib的路徑,沒有任何更改,同樣的錯誤。我已經嘗試使用-headerpad_max_install_names構建測試應用程序,然後使用install_name_tool更改路徑。仍然沒有改變。同樣的錯誤。

是我試圖不可能與Mac操作系統?我是這個平臺的新手,習慣於像Windows和GNU/Linux一樣順利運行。另外,我正在嘗試使用命令行執行所有這些操作。我非常希望避免XCode。

編輯:哎呀,我被製造了。原來,我在我的install_name_tool參數中犯了一個錯字。現在工作正常。

回答

0

您需要確保DYLD_LIBRARY_PATH包含庫駐留的目錄。

DYLD_LIBRARY_PATH 
      This is a colon separated list of directories that contain libraries. The dynamic linker searches these directo- 
      ries before it searches the default locations for libraries. It allows you to test new versions of existing 
      libraries. 

      For each library that a program uses, the dynamic linker looks for it in each directory in DYLD_LIBRARY_PATH in 
      turn. If it still can't find the library, it then searches DYLD_FALLBACK_FRAMEWORK_PATH and DYLD_FALL- 
      BACK_LIBRARY_PATH in turn. 

      Use the -L option to otool(1). to discover the frameworks and shared libraries that the executable is linked 
      against. 

動態鏈接的各種細節記錄在dyld手冊頁中。

0

install_name_tool是正確的工具。 嘗試otool your_binary查看哪個dylib丟失。 還要確保你的二進制文件和鏈接庫是爲相同的體系結構而構建的。