使用g ++ 4.8在Mac OS X上编译boost :: program_options(Mac端口)

hta*_*lor 6 c++ macos gcc boost llvm-gcc

我正在尝试使用gcc 4.8(通过MacPorts安装)编译boost :: program_options示例中的一个,http: //svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp.但是,我不断收到错误:

Undefined symbols for architecture x86_64:
  "boost::program_options::to_internal(std::basic_string, std::allocator > const&)", referenced from:
      std::vector, std::allocator >, std::allocator, std::allocator > > > boost::program_options::to_internal, std::allocator > >(std::vector, std::allocator >, std::allocator, std::allocator > > > const&) in ccEWnIGV.o
  "boost::program_options::options_description::options_description(std::basic_string, std::allocator > const&, unsigned int, unsigned int)", referenced from:
      _main in ccEWnIGV.o
  "boost::program_options::invalid_option_value::invalid_option_value(std::basic_string, std::allocator > const&)", referenced from:
      void boost::program_options::validate(boost::any&, std::vector, std::allocator >, std::allocator, std::allocator > > > const&, double*, long) in ccEWnIGV.o
  "boost::program_options::error_with_option_name::error_with_option_name(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, int)", referenced from:
      boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, int) in ccEWnIGV.o
  "boost::program_options::detail::cmdline::set_additional_parser(boost::function1, std::allocator >, std::basic_string, std::allocator > >, std::basic_string, std::allocator > const&>)", referenced from:
      boost::program_options::basic_command_line_parser::extra_parser(boost::function1, std::allocator >, std::basic_string, std::allocator > >, std::basic_string, std::allocator > const&>) in ccEWnIGV.o
  "boost::program_options::detail::cmdline::cmdline(std::vector, std::allocator >, std::allocator, std::allocator > > > const&)", referenced from:
      boost::program_options::basic_command_line_parser::basic_command_line_parser(int, char const* const*) in ccEWnIGV.o
  "boost::program_options::operator >&, boost::program_options::options_description const&)", referenced from:
      _main in ccEWnIGV.o
  "boost::program_options::abstract_variables_map::operator[](std::basic_string, std::allocator > const&) const", referenced from:
      boost::program_options::variables_map::operator[](std::basic_string, std::allocator > const&) const in ccEWnIGV.o
  "boost::program_options::error_with_option_name::substitute_placeholders(std::basic_string, std::allocator > const&) const", referenced from:
      vtable for boost::program_options::invalid_option_value in ccEWnIGV.o
      vtable for boost::program_options::validation_error in ccEWnIGV.o
etc...

通过MacPorts安装boost库,将头文件放入/opt/local/include和库文件中/opt/local/lib.使用的编译命令是:

$ g++ -I/opt/local/include first.cpp -L/opt/local/lib -lboost_program_options-mt

使用g ++ 4.8编译只有标题的boost函数可以正常工作,即factorial,hermite等.

我觉得奇怪的是,如果我使用llvm-g ++编译器,这个例子会编译:

$ llvm-g++ -I/opt/local/include first.cpp -L/opt/local/lib -lboost_program_options-mt

 $ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.8.1/lto-wrapper
Target: x86_64-apple-darwin12
Thread model: posix
gcc version 4.8.1 (MacPorts gcc48 4.8.1_3)
\
 $ llvm-g++ -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
\

为什么这个例子不用gcc 4.8编译?有任何想法吗?有没有办法来解决这个问题?

更新:该示例还使用clang ++编译

Shi*_*llo 9

我怀疑你正在使用与Clang编译的Boost,这与GCC不兼容.

这是因为C++实现在它们之间有所不同,并且为了区分Clang和更流行的GCC,Clang的STL类被放置在与使用内联命名空间的GCC不同的命名空间中; 例如std::basic_string是std::__1::basic_string在通过锵内嵌命名空间).

尝试下面的命令,如果你在输出中看到"libc ++",是的,你的Boost是用Clang构建的(或者是用于GCC的"libstdc ++"):

$ otool -L /opt/local/lib/libboost_program_options-mt.dylib
/opt/local/lib/libboost_program_options-mt.dylib:
    /opt/local/lib/libboost_program_options-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
Run Code Online (Sandbox Code Playgroud)


如果使用Clang构建Boost,我建议您使用MacPorts提供的GCC-4.8重建Boost:

sudo port upgrade --force boost configure.compiler=macports-gcc-4.8
Run Code Online (Sandbox Code Playgroud)

您可以在https://trac.macports.org/wiki/UsingTheRightCompiler#configure-compiler中找到其他可用的编译器.


其他有用的链接:http: //wiki.inkscape.org/wiki/index.php/CompilingMacOsX#Compiling_with_clang_3.3_and_libc.2B.2B_.28c.2B.2B11_enabled.29_.282013.29


agi*_*agi 5

正如Shigerello已经提到的那样,问题在于使用clang编译的boost和使用gcc编译的boost是不兼容的.

使用GCC-4.8编译获得提升的更简单的变化就是使用boost的gcc48变体:

sudo port install boost +gcc48
Run Code Online (Sandbox Code Playgroud)