C&C++   发布时间:2019-10-05  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – program_options代码中的链接错误与ubuntu上的boost库大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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_options
edd@max:/tmp$./bpoex
1
2
test option
edd@max:/tmp$

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。