【发布时间】: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_<name_of_boost_module_you_need>链接到。 -
我确实忘记为测试程序添加链接,但对于我遇到此问题的真实程序,我确实添加了链接并且问题仍然存在。在包含测试程序的链接标志时,我已经编辑了错误消息。
-
对剩余错误消息的一种可能解释是,涉及两个不同的标准库。您的应用程序是针对不同的 stdlib 构建的,然后 boost 是。
-
那些
cclgNgf3.o、ccmG1kWi.o是链接步骤中编译应用的临时输出文件。由于它们是临时名称,它们会更改。
标签: c++ boost macos-catalina