2012-08-16 36 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