【问题标题】:Linker error with Boost & C++ on OSXOSX 上 Boost 和 C++ 的链接器错误
【发布时间】:2012-08-12 07:52:15
【问题描述】:

我正在使用 Boost 的“program_options”支持编写一个小型 C++ 程序。以下代码:

boost::program_options::options_description desc("Allowed options");
desc.add_options()
    (".refreshrate", boost::program_options::value< int >()->default_value(50), "Delay between frames")
    (".location",    boost::program_options::value<std::string>(&__Location), "Camera Location")
    (".address",     boost::program_options::value<std::string>(&__Address), "Address of Camera")
    ;
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_config_file(ifile, desc, true), vm);
boost::program_options::notify(vm);

编译,但不会链接。我收到神秘的链接错误,例如:

链接 CXX 可执行文件 main 架构 x86_64 的未定义符号: “boost::program_options::validation_error::what() const”,引用自: IEEE1394_Camera.cxx.o 中 boost::program_options::invalid_option_value 的 vtable IEEE1394_Camera.cxx.o 中 boost::exception_detail::clone_impl > 的 vtable IEEE1394_Camera.cxx.o 中 boost::exception_detail::error_info_injector 的 vtable IEEE1394_Camera.cxx.o 中 boost::exception_detail::clone_impl > 的 vtable IEEE1394_Camera.cxx.o 中 boost::exception_detail::error_info_injector 的 vtable “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&)”,引用从: std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool) in IEEE1394_Camera.cxx.o (也许你的意思是: 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& , 整数)) ld:未找到架构 x86_64 的符号 collect2:错误:ld 返回 1 个退出状态

但是,只需删除“.refreshrate”选项,或将其更改为 std::string 而不是 int,即可修复它。

我正在使用 CMake 进行编译,并且我尝试了 Boost 1.49 和 Boost 1.5(我自己编译)。我已经使用 darwin(默认)和 gcc 工具链编译了 Boost,并使用了内置的 gcc4.2 和安装了 Macports 的 4.7 。没有运气。

有什么想法吗?

更新:这是我的完整链接命令(来自“make VERBOSE=1”):

"/Applications/CMake 2.8-8.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1 /opt/local/bin/g++-mp-4.7 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/main.dir/main.cxx.o -o main /Users/rhand/Development/boost-1.50/install/lib /libboost_program_options.a /Users/rhand/Development/boost-1.50/install/lib/libboost_timer.a /Users/rhand/Development/boost-1.50/install/lib/libboost_chrono.a /Users/rhand/Development/boost-1.50 /install/lib/libboost_system.a /Users/rhand/Development/boost-1.50/install/lib/libboost_exception.a

【问题讨论】:

  • 您是否将 Boost.program_options 添加到所需的 Boost 库列表中?这是一个必须链接的编译库。
  • 是的..就像我说的,只要我不打算读取 Int 值,它就可以编译和运行。
  • Boost 新手,所以只是想提供帮助。错误行之一是给你一个建议:boost::program_options::validators::get_single_string(std::vector, std::allocator &gt;, std::allocator, std::allocator &gt; &gt; &gt; const&amp;, bool) in IEEE1394_Camera.cxx.o (*******maybe you meant: boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::basic_string, std::allocator &gt; const&amp;, std::basic_string, std::allocator &gt; const&amp;, int)) 你试过他们认为你的意思吗?
  • 不,因为要更改的行深埋在 Boost 的代码中。所以我怀疑问题出在其他地方。

标签: c++ gcc boost linker


【解决方案1】:

好的,我想通了,虽然我对细节还有些模糊。

我清除了所有内容,从 XCode 回到了 gcc4.2 并使用以下命令重建了 Boost:

./b2 --prefix=/Users/rhand/Development/boost-1.50/install/ --layout=versioned --build-type=complete variant=release link=shared runtime-link=shared threading=single install

然后,我不得不修改我的 CMake 构建中的一些内容以使其正确链接:

set(Boost_COMPILER "-xgcc42")

并设置一些环境变量:

BOOSTROOT=/Users/rhand/Development/boost-1.50/install/
BOOST_INCLUDEDIR=/Users/rhand/Development/boost-1.50/install/include/boost-1_50/
BOOST_LIBRARYDIR=/Users/rhand/Development/boost-1.50/install/lib

然后,一切正常。我不确定问题出在哪里,除非它在指定发布版本或与所有共享库一起使用时有什么特别之处......

【讨论】:

  • 请发布任何对您有帮助的信息来源。很好地回答你自己的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多