Reputation: 3726
I installed Google Test according to this How to properly setup googleTest on OS X aside from XCode. So, lib files ended up in /usr/lib/
.
But I was unable to compile with command
clang++ -I/usr/include -L/usr/lib t.cpp -lgtest
since I have received
ld: library not found for -lgtest
I have found this post ld library path not working under OS X 10.9, so I copied the libraries to another location: /opt/local/lib
. Now I am able to compile my code using
clang++ -I/opt/local/include -L/opt/local/lib/ t.cpp -lgtest
However, I can not remove them from /usr/lib
. If I do, I can not run the compiled program:
dyld: Library not loaded: /usr/local/lib/libgtest.0.dylib
Referenced from: /path_to_code/./a.out
Reason: image not found
Trace/BPT trap: 5
Maybe this is only some setting in my OS?
After being able to compile from command line, I tried from Qt Creator. I added to the project file
INCLUDEPATH += /opt/local/include
LIBS += -L/opt/local/lib -L/usr/lib -lgtest
but I have unresolved symbols:
Undefined symbols for architecture x86_64:
"testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in main.o
"std::string::c_str() const", referenced from:
testing::AssertionResult::message() const in main.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from:
std::string testing::PrintToString<int>(int const&) in main.o
"std::ostream::operator<<(int)", referenced from:
void testing_internal::DefaultPrintNonContainerTo<int>(int const&, std::ostream*) in main.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in main.o
testing::internal::scoped_ptr<std::string>::reset(std::string*) in main.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)", referenced from:
std::string testing::PrintToString<int>(int const&) in main.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream()", referenced from:
std::string testing::PrintToString<int>(int const&) in main.o
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in main.o
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [ut1] Error 1
22:52:06: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ut1 (kit: Desktop Qt 5.3 clang 64bit)
When executing step "Make"
Can somebody explain me what is happening there?
Upvotes: 0
Views: 875
Reputation: 1293
To get Qt Creator in Qt 5.x to generate a Makefile that will work with STL you need to add to your pro file:
CONFIG += stl
Also if you are using C++11 features you need
CONFIG += stl c++11
I notice there seems to be some sort of bug in mkspecs/features/c++14.prf as the result of
CONFIG += stl c++14
...is that the switch
-stdlib=libc++
is missing from the compiler command line during the source file compile step, which means that turning on C++14 support in Qt will defeat the STL switch, and you get link time errors, as far as I can tell. To fix this you can do:
CONFIG += stl c++14
QMAKE_CXXFLAGS += -stdlib=libc++
Which worked for me.
Upvotes: 0
Reputation: 56577
You need to set the environment variable DYLD_LIBRARY_PATH
to the location of the library, so type in the terminal
export DYLD_LIBRARY_PATH=/path/to/your/library
(note: this works only for the current session, put it in your .profile
if you need to work permanently)
I find this extremely annoying, but that's how OS X deals with shared libraries.
As for you second linking error, probably the same issue, you need to specify the environment variable in the Qt Creator (I don't have it installed, but there should be an option somewhere in the project configuration).
Upvotes: 1