Mac OSX,Qt Creator和Google Test

时间:2015-02-15 04:38:15

标签: c++ macos qt

我根据此How to properly setup googleTest on OS X aside from XCode安装了Google Test。因此,lib文件以/usr/lib/结尾。 但我无法用命令编译

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

因为我收到了

ld: library not found for -lgtest

我找到了这篇文章ld library path not working under OS X 10.9,因此我将这些库复制到了另一个位置:/opt/local/lib。现在我可以使用

编译我的代码了
clang++ -I/opt/local/include -L/opt/local/lib/ t.cpp -lgtest

但是,我无法从/usr/lib删除它们。如果我这样做,我就无法运行已编译的程序:

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

也许这只是我操作系统中的一些设置?

能够从命令行编译后,我尝试了Qt Creator。我添加到项目文件

INCLUDEPATH += /opt/local/include
LIBS += -L/opt/local/lib -L/usr/lib -lgtest

但我有未解决的符号:

   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"

有人可以解释一下那里发生了什么吗?

2 个答案:

答案 0 :(得分:1)

您需要将环境变量DYLD_LIBRARY_PATH设置为库的位置,因此键入终端

export DYLD_LIBRARY_PATH=/path/to/your/library

(注意:这仅适用于当前会话,如果您需要永久工作,请将其放在.profile中)

我发现这非常烦人,但这就是OS X处理共享库的方式。

至于你的第二个链接错误,可能是同一个问题,你需要在Qt Creator中指定环境变量(我没有安装它,但在项目配置中应该有一个选项)。

答案 1 :(得分:0)

要在Qt 5.x中获取Qt Creator以生成可与STL一起使用的Makefile,您需要添加到您的专业文件中:

CONFIG += stl

此外,如果您使用C ++ 11功能,则需要

CONFIG += stl c++11

我注意到mkspecs / features / c ++ 14.prf中似乎存在某种错误

CONFIG += stl c++14

...就是开关

-stdlib=libc++
在源文件编译步骤期间,编译器命令行中缺少

,这意味着在Qt中启用C ++ 14支持将使STL交换机失败,并且就我所知,您会收到链接时错误。要解决此问题,您可以执行以下操作:

CONFIG += stl c++14
QMAKE_CXXFLAGS += -stdlib=libc++

这对我有用。