c – program_options代码中的链接错误与ubuntu上的boost库

c – program_options代码中的链接错误与ubuntu上的boost库,第1张

概述我在ubuntu 10.04上安装了boost sudo apt-get install libboost-dev 我想在那之后我不需要设置任何-I和-L标志,所以我编译我的代码 g++ test.cpp 这是我的test.cpp #include <iostream>#include <string>#include <set>#include <sstream>#include <b 我在ubuntu 10.04上安装了boost
sudo apt-get install libboost-dev

我想在那之后我不需要设置任何-I和-L标志,所以我编译我的代码

g++ test.cpp

这是我的test.cpp

#include <iostream>#include <string>#include <set>#include <sstream>#include <boost/config.hpp>#include <boost/program_options/detail/config_file.hpp>#include <boost/program_options/parsers.hpp>namespace pod = boost::program_options::detail;int main() {      //contents    std::stringstream s(            "a = 1\n"            "b = 2\n"            "c = test option\n");    //parameters    std::set<std::string> options;    options.insert("a");    options.insert("b");    options.insert("c");    //parser    for (pod::config_file_iterator i(s,options),e ; i != e; ++i)    {        std::cout << i->value[0] << std::endl;    }}

我认为情况会顺利,但实际上有一些错误

/tmp/ccNQEbJM.o:在函数boost :: program_options :: detail :: basic_config_file_iterator< char> :: basic_config_file_iterator(std :: basic_istream< char,std :: char_traits< char>>&,std :: set< std :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>,std :: less< std :: basic_string< char,std :: char_traits< char&gt ;,std :: allocator< char>>>,std :: allocator< std :: basic_string< char,std :: allocator< char>>>> const&,bool)’:
a.cpp :(.text._ZN5boost15program_options6detail26basic_config_file_iteratorIcEC1ERSiRKSt3setISsst4lessISsESaISsEEb [boost :: program_options :: detail :: basic_config_file_iterator< char> :: basic_config_file_iterator(std :: basic_istream< char,std :: set&lt ; std :: basic_string< char,std :: allocator< ; char>>>,bool)] 0x24): undefined reference toboost :: program_options :: detail :: common_config_file_iterator :: common_config_file_iterator(std :: set,std :: allocator>,std :: less,std :: allocator>&gt ;,std :: allocator,std :: allocator>>> const&,bool)’
a.cpp :(.text._ZN5boost15program_options6detail26basic_config_file_iteratorIcEC1ERSiRKSt3setISsst4lessISsESaISsEEb [boost :: program_options :: detail :: basic_config_file_iterator :: basic_config_file_iterator(std :: basic_istream>&,std :: set,std :: allocator>>,bool)] 0x5f):未定义的引用boost :: program_options :: detail :: common_config_file_iterator :: get( )”
/tmp/ccNQEbJM.o:在functionboost :: eof_iterator> :: increment()’中:
a.cpp :(.text._ZN5boost12eof_iteratorINS_15program_options6detail27common_config_file_iteratorENS1_12basic_optionIcEEE9incrementEv [boost :: eof_iterator> :: increment()] 0x10):对boost :: program_options :: detail :: common_config_file_iterator :: get()’的未定义引用
/tmp/ccNQEbJM.o:在functionboost :: program_options :: detail :: basic_config_file_iterator :: getline(std :: basic_string,std :: allocator>&)’中:
a.cpp :(.text._ZN5boost15program_options6detail26basic_config_file_iteratorIcE7getlineERSS [boost :: program_options :: detail :: basic_config_file_iterator :: getline(std :: basic_string,std :: allocator>&)] 0x5b):未定义的引用`boost :: program_options :: to_internal(std :: basic_string,std :: allocator> const&)’
collect2:ld返回1退出状态

我无法弄清楚出错的地点和原因.

解决方法 您需要链接到Boost program_options库,因为Boost中的所有内容都不是纯模板:
edd@max:/tmp$cat bpoex.cpp #include <iostream>#include <string>#include <set>#include <sstream>#include <boost/config.hpp>#include <boost/program_options/detail/config_file.hpp>#include <boost/program_options/parsers.hpp>namespace pod = boost::program_options::detail;int main() {      //contents    std::stringstream s(            "a = 1\n"            "b = 2\n"            "c = test option\n");    //parameters    std::set<std::string> options;    options.insert("a");    options.insert("b");    options.insert("c");    //parser    for (pod::config_file_iterator i(s,e ; i != e; ++i)    {        std::cout << i->value[0] << std::endl;    }}edd@max:/tmp$g++ -o bpoex bpoex.cpp -lboost_program_optionsedd@max:/tmp$./bpoex12test optionedd@max:/tmp$
总结

以上是内存溢出为你收集整理的c – program_options代码中的链接错误与ubuntu上的boost库全部内容,希望文章能够帮你解决c – program_options代码中的链接错误与ubuntu上的boost库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1241664.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存