Compiling this example code for boost::program_options: http://svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp
为boost:::program_options编译本例代码:http://svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp
...on MacOS Lion (10.7.2), using boost-1.48.0 installed with MacPorts:
…关于MacOS Lion(10.7.2),使用安装在MacPorts的boot -1.48.0:
$ clang++ -v
Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin11.2.0
Thread model: posix
$ clang++ -std=c++0x --stdlib=libc++ -lc++ -I/opt/local/include -L/opt/local/lib -lboost_program_options first.cpp -o first
Undefined symbols for architecture x86_64:
"boost::program_options::options_description::options_description(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, unsigned int)", referenced from:
_main in cc-6QQcwm.o
"boost::program_options::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, boost::program_options::options_description const&)", referenced from:
_main in cc-6QQcwm.o
"boost::program_options::abstract_variables_map::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
boost::program_options::variables_map::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const in cc-6QQcwm.o
"boost::program_options::detail::cmdline::set_additional_parser(boost::function1<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>)", referenced from:
boost::program_options::basic_command_line_parser<char>::extra_parser(boost::function1<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>) in cc-6QQcwm.o
"boost::program_options::detail::cmdline::cmdline(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)", referenced from:
boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*) in cc-6QQcwm.o
"boost::program_options::to_internal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > boost::program_options::to_internal<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) in cc-6QQcwm.o
"boost::program_options::invalid_option_value::invalid_option_value(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
void boost::program_options::validate<int, char>(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, int*, long) in cc-6QQcwm.o
"boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const& boost::program_options::validators::get_single_string<char>(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool) in cc-6QQcwm.o
"boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool) const", referenced from:
vtable for boost::program_options::typed_value<int, char> in cc-6QQcwm.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The same code compiled/linked with g++4.7 installed with MacPorts:
与MacPorts安装的g++4.7编译/链接的代码相同:
$ g++-mp-4.7 -std=c++0x -I/opt/local/include -L/opt/local/lib -lboost_program_options -o first first.cpp
... works fine. As does using clang without libc++:
…工作很好。使用没有libc++的clang也一样:
clang++ -std=c++0x -I/opt/local/include -L/opt/local/lib -lboost_program_options first.cpp -o first
What's wrong? Why does boost::program_options and libc++ not work together?
怎么了?为什么boost::program_options和libc++不一起工作?
1 个解决方案
#1
130
You need to rebuild boost using clang++ -stdlib=libc++.
您需要使用clang++ -stdlib=libc++重建boost。
libc++ is not binary compatible with gcc's libstdc++ (except for some low level stuff such as operator new). For example the std::string
in gcc's libstdc++ is refcounted, whereas in libc++ it uses the "short string optimization". If you were to accidentally mix these two strings in the same program (and mistake them for the same data structure), you would inevitably get a run time crash.
libc++ +与gcc的libstdc++不兼容(除了一些低级的东西,如操作符new)。例如,在gcc的libstdc++中,std::string被重新计算,而在libc++ +中,它使用“短字符串优化”。如果您在同一个程序中意外地混合了这两个字符串(并将它们误认为相同的数据结构),您将不可避免地导致运行时崩溃。
This accident is exactly what has occurred in your case.
这一事故正是发生在你身上的。
In order to turn this run time crash into a link time error, libc++ uses a C++11 language feature called inline namespace
to change the ABI of std::string
without impacting the API of std::string
. That is, to you std::string
looks the same. But to the linker, std::string
is being mangled as if it is in namespace std::__1
. Thus the linker knows that std::basic_string
and std::__1::basic_string
are two different data structures (the former coming from gcc's libstdc++ and the latter coming from libc++).
为了将这个运行时崩溃转换成一个链接时间错误,libc++使用一个名为inline namespace的c++语言特性来更改std::string的ABI,而不会影响std:::string的API。也就是说,std::字符串看起来是一样的。但是对于链接器来说,std::string正在被分解,就好像它在名称空间std::__1中一样。因此链接器知道std::basic_string和std: __1: basic_string是两个不同的数据结构(前者来自gcc的libstdc++ ++,后者来自libc++ +)。
#1
130
You need to rebuild boost using clang++ -stdlib=libc++.
您需要使用clang++ -stdlib=libc++重建boost。
libc++ is not binary compatible with gcc's libstdc++ (except for some low level stuff such as operator new). For example the std::string
in gcc's libstdc++ is refcounted, whereas in libc++ it uses the "short string optimization". If you were to accidentally mix these two strings in the same program (and mistake them for the same data structure), you would inevitably get a run time crash.
libc++ +与gcc的libstdc++不兼容(除了一些低级的东西,如操作符new)。例如,在gcc的libstdc++中,std::string被重新计算,而在libc++ +中,它使用“短字符串优化”。如果您在同一个程序中意外地混合了这两个字符串(并将它们误认为相同的数据结构),您将不可避免地导致运行时崩溃。
This accident is exactly what has occurred in your case.
这一事故正是发生在你身上的。
In order to turn this run time crash into a link time error, libc++ uses a C++11 language feature called inline namespace
to change the ABI of std::string
without impacting the API of std::string
. That is, to you std::string
looks the same. But to the linker, std::string
is being mangled as if it is in namespace std::__1
. Thus the linker knows that std::basic_string
and std::__1::basic_string
are two different data structures (the former coming from gcc's libstdc++ and the latter coming from libc++).
为了将这个运行时崩溃转换成一个链接时间错误,libc++使用一个名为inline namespace的c++语言特性来更改std::string的ABI,而不会影响std:::string的API。也就是说,std::字符串看起来是一样的。但是对于链接器来说,std::string正在被分解,就好像它在名称空间std::__1中一样。因此链接器知道std::basic_string和std: __1: basic_string是两个不同的数据结构(前者来自gcc的libstdc++ ++,后者来自libc++ +)。