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

前端之家收集整理的这篇文章主要介绍了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$
原文链接:https://www.f2er.com/c/119390.html

猜你在找的C&C++相关文章