Reputation: 3726
I installed Google Test according to How to properly setup googleTest on OS X aside from XCode. I am trying to run
clang++ -I/usr/include -L/usr/lib t.cpp -lgtest
and I am receiving the error:
ld: library not found for -lgtest
However, the library exists:
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
What am I doing wrong?
Here is the complete output:
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)
Upvotes: 0
Views: 3727
Reputation: 90551
You have installed Xcode but not the Command Line Tools package. Normally, this would be OK. However, when building like that, the compiler needs to reference the standard headers and libraries from within the Xcode app bundle. It does this by applying a default -isysroot
option of /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
.
That causes it to look in the usr/lib
directory inside that SDK. But your libgtest binaries are not in that subdirectory in the SDK because they are custom libraries. (By the way, it is very bad form to install any third-party libraries in /usr/lib
. Third-party stuff should go in /usr/local/lib
or something like /opt/local/lib
.)
I recommend that you reinstall libgtest with a prefix of /usr/local
or something like that, then pass -L/usr/local/lib
to your compile command. Or you can install the Command Line Tools package and try the build again.
It's possible that installing the Command Line Tools will wipe out any custom libraries you have installed in /usr/lib
. So, you may end up needing to reinstall libgtest, anyway.
Upvotes: 3