gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:54:59 25 4
gpt4 key购买 nike

我正在使用 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);

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

Linking CXX executable main Undefined symbols for architecture x86_64: "boost::program_options::validation_error::what() const", referenced from: vtable for boost::program_options::invalid_option_value in IEEE1394_Camera.cxx.o vtable for boost::exception_detail::clone_impl > in IEEE1394_Camera.cxx.o vtable for boost::exception_detail::error_info_injector in IEEE1394_Camera.cxx.o vtable for boost::exception_detail::clone_impl > in IEEE1394_Camera.cxx.o vtable for boost::exception_detail::error_info_injector 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&)", referenced from: 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 (maybe you meant: 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)) ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status

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

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

有什么想法吗?

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

最佳答案

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

我清除了所有内容,从 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

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

关于c++ - OSX 上 Boost 和 C++ 的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11979123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com