【问题标题】:Mac OSX, Qt Creator and Google TestMac OSX、Qt Creator 和 Google 测试
【发布时间】:2015-02-15 04:38:15
【问题描述】:

我根据这个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"

谁能解释一下那里发生了什么?

【问题讨论】:

    标签: c++ macos qt


    【解决方案1】:

    需要将环境变量DYLD_LIBRARY_PATH设置为库所在的位置,所以在终端输入

    export DYLD_LIBRARY_PATH=/path/to/your/library

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

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

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

    【讨论】:

    • 谢谢vsoftco,export命令解决了命令行问题。我仍在寻找 Qt Creator 解决方案。
    【解决方案2】:

    要让 Qt 5.x 中的 Qt Creator 生成可与 STL 一起使用的 Makefile,您需要将其添加到您的 pro 文件中:

    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++
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多