ld库路径在OS X 10.9下不起作用

时间:2014-08-18 22:31:22

标签: c++ xcode osx-mavericks

我一直在试图找出g++无法将程序与armadillo库链接的原因。问题很简单:

macbook-pro:arma-xc jmlopez$ g++-4.9 inputs-arma.cpp -larmadillo
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status

也许我确实指定了库应该在哪里

macbook-pro:arma-xc jmlopez$ ls -l /usr/lib/libarma*
-rwxr-xr-x  1 root  wheel  37928 Aug 18 14:30 /usr/lib/libarmadillo.4.32.2.dylib
lrwxr-xr-x  1 root  wheel     25 Aug 18 14:30 /usr/lib/libarmadillo.4.dylib -> libarmadillo.4.32.2.dylib
lrwxr-xr-x  1 root  wheel     20 Aug 18 14:30 /usr/lib/libarmadillo.dylib -> libarmadillo.4.dylib

让我们再试一次

macbook-pro:arma-xc jmlopez$ g++-4.9 -L/usr/lib inputs-arma.cpp -larmadillo
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status

好的,这没有解决它。让我们看看发生了什么,为此我们将使用-v选项,它将生成一些长输出。

macbook-pro:arma-xc jmlopez$ g++-4.9 -L/usr/lib inputs-arma.cpp -larmadillo -v
Using built-in specs.
COLLECT_GCC=g++-4.9
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/lto-wrapper
Target: x86_64-apple-darwin13.3.0
Configured with: ../configure --build=x86_64-apple-darwin13.3.0 --prefix=/usr/local/Cellar/gcc/4.9.1 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion='Homebrew gcc 4.9.1' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
Thread model: posix
gcc version 4.9.1 (Homebrew gcc 4.9.1) 
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.3' '-L/usr/lib' '-v' '-shared-libgcc' '-mtune=core2'
 /usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/cc1plus -quiet -v -D__DYNAMIC__ inputs-arma.cpp -fPIC -quiet -dumpbase inputs-arma.cpp -mmacosx-version-min=10.9.3 -mtune=core2 -auxbase inputs-arma -version -o /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//ccApA95Q.s
GNU C++ (Homebrew gcc 4.9.1) version 4.9.1 (x86_64-apple-darwin13.3.0)
    compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR version 3.1.2-p8, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/local/include"
ignoring nonexistent directory "/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../../../x86_64-apple-darwin13.3.0/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/jmlopez/github/excentury/excentury/extern/include
 /usr/include
 /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++
 /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/x86_64-apple-darwin13.3.0
 /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/backward
 /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include
 /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include-fixed
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks
End of search list.
GNU C++ (Homebrew gcc 4.9.1) version 4.9.1 (x86_64-apple-darwin13.3.0)
    compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR version 3.1.2-p8, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 5a051bcf2be886a5c6eb8ba33f338693
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.3' '-L/usr/lib' '-v' '-shared-libgcc' '-mtune=core2'
 as -arch x86_64 -force_cpusubtype_ALL -o /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//cchm9rVF.o /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//ccApA95Q.s
COMPILER_PATH=/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/
LIBRARY_PATH=/usr/lib/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../../:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.3' '-L/usr/lib' '-v' '-shared-libgcc' '-mtune=core2'
 /usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/collect2 -dynamic -arch x86_64 -macosx_version_min 10.9.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -weak_reference_mismatches non-weak -o a.out -L/usr/lib -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1 -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../.. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//cchm9rVF.o -larmadillo -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 4.9.1
/usr/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.9.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -weak_reference_mismatches non-weak -o a.out -L/usr/lib -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1 -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../.. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//cchm9rVF.o -larmadillo -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
@(#)PROGRAM:ld  PROJECT:ld64-236.4
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 armv6m armv7m armv7em
Library search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
    /usr/local/Cellar/gcc/4.9.1/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Framework search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status

重要信息如下:

Library search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
    /usr/local/Cellar/gcc/4.9.1/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib

不知怎的,它没有查看/usr/lib。我先尝试过指定这个目录 导出变量LD_LIBRARY_PATH

macbook-pro:arma-xc jmlopez$ export LD_LIBRARY_PATH=/usr/lib
macbook-pro:arma-xc jmlopez$ g++-4.9 -L/usr/lib inputs-arma.cpp -larmadillo
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status

没有运气。最后,我认为ld可能会被打破:

macbook-pro:arma-xc jmlopez$ ln -s /usr/lib/libarmadillo.dylib /usr/local/Cellar/gcc/4.9.1/lib/libarmadillo.dylib
macbook-pro:arma-xc jmlopez$ g++-4.9 inputs-arma.cpp -larmadillo

现在它有效。我不确定我的系统是否有问题,但似乎我没有办法为ld指定其他路径来查找我的库。相反,我必须使用以下任何一种方法:

Library search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
    /usr/local/Cellar/gcc/4.9.1/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib

有没有办法解决这个问题?请注意,我正在使用自制软件中的g++,因为我认为xcode附带的那个被破坏了。无论如何,没有运气,至少自制软件g++告诉我搜索图书馆的路径。

更新

我决定看看是否可以添加另一条路径:

g++-4.9 -L/Users/jmlopez/ -L/Users/jmlopez/Desktop -L/usr/lib inputs-arma.cpp -larmadillo -v

现在,它搜索库的路径列表是:

Library search paths:
    /Users/jmlopez/
    /Users/jmlopez/Desktop
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
    /usr/local/Cellar/gcc/4.9.1/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib

/usr/lib在哪里?这个目录有什么特别之处ld拒绝在这个目录中查找。它在我的主目录和我的桌面中搜索。我尝试添加/usr/local并将其添加到目录列表中。我现在开始怀疑目录的权限......有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您的自制g ++可能是使用自定义前缀编译的。 gcc的前缀通常是/ usr,这意味着它将在/ usr / bin中查找二进制文件,在/ usr / include中查找/ / usr / lib中的库。如果您使用自定义前缀编译(在这种情况下似乎是/usr/local/Cellar/gcc/4.9.1/,似乎)它不会在/ usr / lib中查找。

LD_LIBRARY_PATH是告诉运行时链接器在哪里查找库的方法。因此,如果您已链接到/usr/my/bizarre/lib/path/libmylib.so,那么您可以像这样运行它:

> LD_LIBRARY_PATH=/usr/my/bizarre/lib/path myprog

要告诉g ++在哪里找到库,可以使用-L命令行选项。所以你的命令行应该是这样的:

g++-4.9 inputs-arma.cpp -L/usr/lib -larmadillo

答案 1 :(得分:0)

我遇到了这个线程试图自己回答类似的问题。希望这会有所帮助。

你需要使用ld_library_path来运行自制程序编译器,并使用dyld_library_path使osx正确使用该库。

将以下出口放在他们可以获得来源的地方。

export LD_LIBRARY_PATH = / usr / lib:$ LD_LIBRARY_PATH

export DYLD_LIBRARY_PATH = $ LD_LIBRARY_PATH:$ DYLD_LIBRARY_PATH

在OS X 10.9和10.10上使用自制软件gcc进行编译时,这对我有用。

答案 2 :(得分:0)

这样做的原因是Xcode将SDK(在您的情况下为macOS SDK)设置为虚拟根目录。因此,编译器和链接器的所有搜索路径都被解释为相对于SDK文件夹而言。

具体地说,链接器将不在/ usr / lib中,而将在[macOS SDK] / usr / lib中。

如果您需要使用/ usr / lib(例如,当链接到仅存在于/ usr / lib中但不存在于[macOS SDK] / usr / lib中的OpenSSL的libssl时,可以将“ -isysroot /”设置为第一个)项目或目标的构建设置的两个“其他链接器标志”,以确保链接器将/用作根(因此将在/ usr / lib中找到库),而编译器将继续使用由设置的虚拟根Xcode(即macOS SDK)。