【问题标题】:Boost on Mac OS Undefined Symbols增强 Mac OS 未定义符号
【发布时间】:2020-03-13 15:28:39
【问题描述】:

我在编译包含 boost 库的 c++ 程序时遇到问题。在我更新到 Catalina 之后,在一切正常之前,问题似乎已经开始出现。

我使用通过 homebrew 安装的 boost ("brew install boost") 并使用 g++ 版本 9 als 与 homebrew 一起编译程序。

使用“g++-9 -I/usr/local/include -L/usr/local/lib -lboost_serialization test.cc”编译时,编译器可以正确找到库,但会出现以下错误。下面我包括错误消息和导致它的测试程序。

一个有趣的观察是,每次编译时,错误消息中引用为“cclgNgf3.o”的名称都会发生变化。到目前为止,我还得到了“ccmG1kWi.o”和“cc25AYXh.o”。

Undefined symbols for architecture x86_64:
  "boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(boost::archive::text_oarchive&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in cclgNgf3.o
  "boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::basic_ostream<char, std::char_traits<char> >&, unsigned int)", referenced from:
      boost::archive::text_oarchive::text_oarchive(std::basic_ostream<char, std::char_traits<char> >&, unsigned int) in cclgNgf3.o
  "boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()", referenced from:
      boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl() in cclgNgf3.o
      boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl() in cclgNgf3.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
#include <boost/archive/text_oarchive.hpp>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    boost::archive::text_oarchive oa{cout};
    return 0;
}

如果通过调用“g++ -lboost_serialization test.cc”使用 macOS clang 编译器进行编译可能相关,则会打印以下错误消息。

test.cc:8:32: error: no matching constructor for initialization of 'boost::archive::text_oarchive'
        boost::archive::text_oarchive oa{cout};
                                      ^
/usr/local/include/boost/archive/text_oarchive.hpp:98:28: note: candidate constructor (the implicit copy constructor) not viable: requires 1
      argument, but 0 were provided
class BOOST_SYMBOL_VISIBLE text_oarchive : 
                           ^
/usr/local/include/boost/archive/text_oarchive.hpp:102:5: note: candidate constructor not viable: requires at least argument 'os_', but no arguments
      were provided
    text_oarchive(std::ostream & os_, unsigned int flags = 0) :
    ^
test.cc:8:34: error: expected ';' at end of declaration
        boost::archive::text_oarchive oa{cout};
                                        ^
                                        ;
2 errors generated.

【问题讨论】:

  • 您的-I/usr/local/include 允许编译器找到库的标头,从而允许代码编译,但只要库不是仅标头,那么该库也有已编译的部分。你需要告诉哪些库将-lboost_&lt;name_of_boost_module_you_need&gt;链接到。
  • 我确实忘记为测试程序添加链接,但对于我遇到此问题的真实程序,我确实添加了链接并且问题仍然存在。在包含测试程序的链接标志时,我已经编辑了错误消息。
  • 对剩余错误消息的一种可能解释是,涉及两个不同的标准库。您的应用程序是针对不同的 stdlib 构建的,然后 boost 是。
  • 那些cclgNgf3.occmG1kWi.o是链接步骤中编译应用的临时输出文件。由于它们是临时名称,它们会更改。

标签: c++ boost macos-catalina


【解决方案1】:

我使用 gcc 而不是 clang 手动构建了 boost,现在它可以工作了。看来brew版本是用clang构建的,所以当我尝试使用gcc构建我的项目时,我们会遇到标准库的冲突。

【讨论】:

  • 嗨@ramkick,感谢您跟进您自己的问题。您能否提供有关手动构建提升的更多详细信息?谢谢。
  • 我认为我按照github.com/liballeg/allegro5 的说明进行操作
  • 感谢您的快速回复!但是,allegro5 似乎并不依赖于 boost。我找不到任何与之相关的说明。可能是另一个项目?
  • 确实,我指的是另一个项目。我不记得确切的构建过程,但我想我遵循了官方文档boost.org/doc/libs/1_64_0/doc/html/bbv2.html
猜你喜欢
  • 2013-06-10
  • 1970-01-01
  • 2012-04-26
  • 2017-08-21
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
相关资源
最近更新 更多