2012-08-16 136 views
0

我正在使用Boost的'program_options'支持编写一个小型的C++程序。以下代码:OSX上的Boost和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 ::字符串,而不是一个int,修复它。

我正在编译CMake,我试过Boost 1.49和Boost 1.5(自己编译)。我已经与达尔文(默认)和gcc工具链共同使用Boost,并使用内置的gcc4.2和Macports安装的4.7。没有运气。

任何想法?

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

+0

您是否将Boost.program_options添加到所需的Boost库列表中?这是一个你必须链接的编译库。 – 2012-08-16 00:32:00

+0

是的..就像我说的,它编译和运行就好,只要我不打算读取Int值。 – Yeraze 2012-08-16 00:56:32

+0

新的促进,所以只是想帮助。其中一个错误行给你一个建议:'boost :: program_options :: validators :: get_single_string(std :: vector,std :: allocator>,std :: allocator,std :: allocator>>> const&,bool)在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&,int))'你有没有试过他们认为你可能意味着什么? – ChiefTwoPencils 2012-08-16 02:33:19

回答

3

好吧,我想通了这一点,但我还是有点模糊,以细节。

我抹去一切了,又回到了股票gcc4.2从Xcode和使用这个命令重建加速:

./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 

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

+0

请发布任何信息来源,如果有帮助你。好的工作回答你自己的。 – ChiefTwoPencils 2012-08-16 02:47:41