OSX 上的 Boost 和 C++ 链接器错误

Linker error with Boost & C++ on OSX

本文关键字:链接 错误 C++ 上的 Boost OSX      更新时间:2023-10-16

我正在使用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 可执行文件主 建筑的未定义符号x86_64: "boost::p rogram_options::validation_error::what() const",引用自: vtable for boost::p rogram_options::invalid_option_value in IEEE1394_Camera.cxx.o vtable for boost::exception_detail::clone_impl> in IEEE1394_Camera.cxx.o 用于提升的 vtable::exception_detail::error_info_injector in IEEE1394_Camera.cxx.o vtable for boost::exception_detail::clone_impl> in IEEE1394_Camera.cxx.o 用于提升的 vtable::exception_detail::error_info_injector in IEEE1394_Camera.cxx.o "boost::p rogram_options::validation_error::validation_error(boost::p rogram_options::validation_error::kind_t, std::basic_string, std:::allocator> const&, std::basic_string, std::allocator> const&)",引用自: std::basic_string, std::allocator> const& boost::p rogram_options::validators::get_single_string(std::vector, std::allocator>, std::allocator, std::allocator>>> const&, bool) in IEEE1394_Camera.cxx.o (也许你的意思是:boost::p rogram_options::validation_error::validation_error(boost::p rogram_options::validation_error::kind_t, std::basic_string, std::allocator> const&, std::basic_string, std::allocator> const&, int)) LD:在建筑x86_64中找不到符号 collect2:错误:ld 返回 1 个退出状态

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

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

有什么想法吗?

更新:这是我的完整链接命令(来自"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_例外。

好的,我想通了,尽管我对细节仍然有点模糊。

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

./b2 --前缀=/用户/rhand/开发/boost-1.50/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

然后,一切正常。 我不完全确定问题是什么,除非它在指定发布版本或使用所有共享库时有什么特殊之处......