-1

I need help on , I have been doing this on the same computer, it means I already installed boost library, and based on the previous code, but this time it gives me errors: /tmp/ccpAYzPw.o: In function `main':

reading_data.cpp:(.text+0x356): undefined reference to `boost::program_options::options_description::m_default_line_length'
reading_data.cpp:(.text+0x361): undefined reference to `boost::program_options::options_description::m_default_line_length'
reading_data.cpp:(.text+0x3a6): undefined reference to `boost::program_options::options_description::options_description(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)'
reading_data.cpp:(.text+0x3d3): undefined reference to `boost::program_options::options_description::add_options()'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'reading_data' failed

I spent almost 2 hour to see what is going on? but I could not understand why, so I need your helps.

Here is my code and thank you very much.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>      // std::ifstream
#include <boost/program_options.hpp>

int main ()
{
    boost::program_options::options_description desc("Allowed options");

    desc.add_options()
            ("sign"  , program_options::value<string>()  -> default_value("gbm")    ,"name of the input")
            ("week"  , program_options::value<double>()  -> default_value(1930)     ,"number of the week")
            ("day"   , program_options::value<double>()  -> default_value(0)        ,"number of the day in within the week")
            ("hour"  , program_options::value<double>()  -> default_value(0)        ,"time in hour")
            ("minute", program_options::value<double>()  -> default_value(0)        ,"time in minute")
            ("second", program_options::value<double>()  -> default_value(0)        ,"time in second")
            ;

    cout << "Done!" << endl;
    return 0;
}
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416

3 Answers3

7

It seems that you are not linking the library. boost::program_options is not header-only, so you have to explicitly link it:

-lboost_program_options
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • I am sorry, I do not understand what you meant? – john nguyen Jan 25 '17 at 15:23
  • 1
    Some parts of boost are compiled as libraries. This is one of them. You need to add settings to your linker options. He gave you the linker option for that. – drescherjm Jan 25 '17 at 15:23
  • @johnnguyen: in order to use "pre-compiled" libraries you need to **link** them to your project. This is a good explanation: http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – Vittorio Romeo Jan 25 '17 at 15:24
  • Exactly what I already stated in my comment, you need to LINK the boost_program_options library when building your program. If you don't know what linking is all about, you can read here: http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work – roalz Jan 25 '17 at 15:24
  • Including a header file may not contain the *implementation* of functions. You will need to build the boost library which contains the implementation. – Thomas Matthews Jan 25 '17 at 15:25
2

You accidently added a superfluous semicolon at the end of the following line:

desc.add_options(); // was a typo in the original post

You also need to declare variables for storing the input options.

std::string sign;
double week, day, hour, minute, second;
desc.add_options()
        ("sign"  , program_options::value<std::string>(&sign) -> default_value("gbm")    ,"name of the input")
        ("week"  , program_options::value<double>(&week)      -> default_value(1930)     ,"number of the week")
        ("day"   , program_options::value<double>(&day)       -> default_value(0)        ,"number of the day in within the week")
        ("hour"  , program_options::value<double>(&hour)      -> default_value(0)        ,"time in hour")
        ("minute", program_options::value<double>(&minute)    -> default_value(0)        ,"time in minute")
        ("second", program_options::value<double>(&second)    -> default_value(0)        ,"time in second")
        ;
vre
  • 6,041
  • 1
  • 25
  • 39
1

Boost program_options is a compiled library, not an header-only like many other boost libraries.
So you need to link the library to your program when building.

How to do that highly depends on the compiler and platform you are using.

If you don't know what linking is all about, you can read here: How does the compilation/linking process work?

Community
  • 1
  • 1
roalz
  • 2,699
  • 3
  • 25
  • 42