Link error in Boost program_options code on Ubuntu

前端 未结 1 659
南笙
南笙 2021-01-17 17:17

I installed boost on ubuntu 10.04 by

sudo apt-get install libboost-dev

I think after that I don\'t need to set any -I and -L flags, so I co

相关标签:
1条回答
  • 2021-01-17 17:56

    You need to link to the Boost program_options library as not everything in Boost is pure templates:

    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, options), 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$ 
    
    0 讨论(0)
提交回复
热议问题