Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link error in Boost program_options code on Ubuntu

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 compile my code by

g++ test.cpp

Here is my 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;
    }
}

I think things will goes well but actually there are some errors:

/tmp/ccNQEbJM.o: In function `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>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<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::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>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool)]+0x24): undefined reference to `boost::program_options::detail::common_config_file_iterator::common_config_file_iterator(std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<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::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>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool)]+0x5f): undefined reference to `boost::program_options::detail::common_config_file_iterator::get()'
/tmp/ccNQEbJM.o: In function `boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >::increment()':
a.cpp:(.text._ZN5boost12eof_iteratorINS_15program_options6detail27common_config_file_iteratorENS1_12basic_optionIcEEE9incrementEv[boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >::increment()]+0x10): undefined reference to `boost::program_options::detail::common_config_file_iterator::get()'
/tmp/ccNQEbJM.o: In function `boost::program_options::detail::basic_config_file_iterator<char>::getline(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
a.cpp:(.text._ZN5boost15program_options6detail26basic_config_file_iteratorIcE7getlineERSs[boost::program_options::detail::basic_config_file_iterator<char>::getline(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)]+0x5b): undefined reference to `boost::program_options::to_internal(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: ld returned 1 exit status

I can't figure out where and why I went wrong.

like image 685
light13 Avatar asked Dec 28 '11 05:12

light13


People also ask

Where is boost on Ubuntu?

For me, the library path for boost was in /usr/lib64 , and that's all I had to add to my library path for the code to build properly. On a different machine, I found it installed under /usr/lib . Save this answer. libboost-dev depends on libboost1.

Why use boost C++?

Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications.

What is boost in linux?

Boost is a set of libraries for the C++ programming language that provides support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains 164 individual libraries (as of version 1.76).

Is boost a standard C++ library?

The Boost C++ Libraries are a collection of modern libraries based on the C++ standard. The source code is released under the Boost Software License, which allows anyone to use, modify, and distribute the libraries for free.


1 Answers

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$ 
like image 175
Dirk Eddelbuettel Avatar answered Oct 21 '22 09:10

Dirk Eddelbuettel