2012-04-17 48 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丢失。 还要确保你的二进制文件和链接库是为相同的体系结构而构建的。