提升 Mac OS 未定义符号
Boost on Mac OS Undefined Symbols
我在编译包含 boost 库的 C++ 程序时遇到问题。在我更新到 Catalina 之后,在一切正常之前,问题似乎已经开始出现。
我使用通过自制软件安装的 boost ("brew install boost") 并使用 g++ 版本 9 编译程序并安装了自制软件。
当使用 "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.
我使用 gcc 而不是 clang 手动构建了 boost,现在它可以工作了。看起来 brew 版本是用 clang 构建的,所以当我尝试使用 gcc 构建我的项目时,我们得到了标准库的冲突。
我在编译包含 boost 库的 C++ 程序时遇到问题。在我更新到 Catalina 之后,在一切正常之前,问题似乎已经开始出现。
我使用通过自制软件安装的 boost ("brew install boost") 并使用 g++ 版本 9 编译程序并安装了自制软件。
当使用 "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.
我使用 gcc 而不是 clang 手动构建了 boost,现在它可以工作了。看起来 brew 版本是用 clang 构建的,所以当我尝试使用 gcc 构建我的项目时,我们得到了标准库的冲突。