0

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)
Community
  • 1
  • 1
robert
  • 3,539
  • 3
  • 35
  • 56
  • Add `-v` to the command and show the complete output of that. – Ken Thomases Feb 14 '15 at 17:53
  • It's looking inside your Xcode install not in /usr/lib. You could try pointing your system root at "/" with `isysroot /` – nielsbot Feb 14 '15 at 18:06
  • I found this: http://stackoverflow.com/questions/25373055/ld-library-path-not-working-under-os-x-10-9 It seems the problem is an OSX issue. – robert Feb 14 '15 at 18:10
  • Adding to nielsbot's comment: If you used Xcode to call clang, you can set "-isysroot /" as the first two "Other Linker Flags" of your project's or target's build settings. This ensures that the Linker will use / as the root (and hence will find libraries in /usr/lib), while the compiler will continue to use the virtual root set by Xcode (i.e. the macOS SDK). This worked for me when the linker did not find libssl (which exists in /usr/lib, but not in /usr/lib). – Piers Uso Walter Oct 07 '19 at 08:30

1 Answers1

3

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154