Mac OSX:ld找不到现有的库

时间:2015-02-14 17:26:58

标签: macos clang

我根据How to properly setup googleTest on OS X aside from XCode安装了Google测试版。我正在尝试运行

clang++ -I/usr/include -L/usr/lib t.cpp -lgtest

我收到错误:

ld: library not found for -lgtest

但是,该库存在:

ls /usr/lib/libgtest*
/usr/lib/libgtest.0.dylib
/usr/lib/libgtest.a
/usr/lib/libgtest.dylib
/usr/lib/libgtest.la
/usr/lib/libgtest.lai         
/usr/lib/libgtest_main.la
/usr/lib/libgtest_main.0.dylib
/usr/lib/libgtest_main.a
/usr/lib/libgtest_main.dylib
/usr/lib/libgtest_main.la
/usr/lib/libgtest_main.lai

我做错了什么?

这是完整的输出:

clang++ -I/usr/include -L/usr/lib/ t.cpp -lgtest -v
       Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
       Target: x86_64-apple-darwin13.4.0
       Thread model: posix
       "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit   -obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name t.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -mu   nwind-tables -target-cpu core2 -target-linker-version 241.9 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xct   oolchain/usr/bin/../lib/clang/6.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -I /   usr/include -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/robi/Work2014/ut -ferror-limit 19 -fmessage-length 110 -stack-protecto   r 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option    -fcolor-diagnostics -vectorize-slp -o /var/folders/nm/drft6q4n64vckdwz4lpt48_00000gn/T/t-942578.o -x c++ t.cpp
       clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin13.4.0
       ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/incl   ude/c++/v1"
       ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/loca   l/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:
       /usr/include
       /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
       /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
       /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
       /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 (framework directo   ry)
       End of search list.
       "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version   _min 10.9.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -o a.out -L/usr/lib/ /va   r/folders/nm/drft6q4n64vckdwz4lpt48_00000gn/T/t-942578.o -lgtest -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.   xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
       ld: library not found for -lgtest
   clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:2)

您已安装Xcode但未安装命令行工具包。通常,这没关系。但是,当像这样构建时,编译器需要从Xcode应用程序包中引用标准头文件和库。它通过应用-isysroot的默认/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk选项来实现此目的。

这导致它查看该SDK中的usr/lib目录。但是你的libgtest二进制文件不在SDK的子目录中,因为它们是自定义库。 (顺便说一句,在/usr/lib中安装任何第三方库是非常糟糕的形式。第三方内容应该放在/usr/local/lib或类似/opt/local/lib的内容中。)

我建议你重新安装前缀为/usr/local的libgtest或类似的东西,然后将-L/usr/local/lib传递给你的编译命令。或者,您可以安装命令行工具包并再次尝试构建。

可能安装命令行工具将清除您在/usr/lib中安装的所有自定义库。因此,无论如何,您最终可能需要重新安装libgtest。