1

I'm trying to follow this tutorial on boost program_options, but I'm getting this error:

error: 'desc' does not name a type.

Here is the source code I have:

#include <boost/program_options.hpp>

using namespace std;

namespace po = boost::program_options;

po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("compression", po::value<int>(), "set compression level")
;

int main()
{
    return 0;
}

the error is on the line starting with 'desc.add_options', not the line where I construct it.

/usr/local/boost is my BOOST_ROOT, and I have it added to my code blocks compiler settings. The compiler arg is -I/usr/local/boost

Why is this not working?

msknapp
  • 1,595
  • 7
  • 22
  • 39

1 Answers1

4

Looks like you try to use the lines

po::options_description desc("Allowed options");
desc.add_options()

on top level outside of all functions. In C++ this does not work - move this code to a function.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • I put it in the main method, now I am getting a whole lot more errors: undefined reference to 'boost::program_options:options_description::m_default_line_length'. I must still be missing something, I have no idea what. – msknapp Aug 10 '14 at 23:44
  • 1
    You need to link with program_options. In gcc that would be -lboost_program_options. – Wojtek Surowka Aug 10 '14 at 23:49
  • the boost instructions just said I need to have the boost directory given by -I/usr/local/boost, where I installed it. – msknapp Aug 10 '14 at 23:50
  • http://stackoverflow.com/questions/12179154/undefined-reference-to-boostprogram-optionsoptions-descriptionm-default-l – Wojtek Surowka Aug 10 '14 at 23:51
  • ok so in Code::Blocks, I tried adding that to my linker settings tab under "Other linker options", now my build log just says "/usr/bin/ld: cannot find -lboost_program_options" and it failed. – msknapp Aug 10 '14 at 23:53
  • I don't now Code::Blocks, sorry. You should look there for option to add libraries as linker input, and add boost_program_options. – Wojtek Surowka Aug 11 '14 at 00:00
  • do you think the problem could have to do with how I installed boost? I simply downloaded their tarball and untarred it into /usr/local. Do you think I need to do some kind of 'sudo apt-get install boost' command instead? – msknapp Aug 11 '14 at 00:10
  • Yes I think it is not only about downloading. Most of boost should be usable after it since only header files are needed. But program_options needs to be build to create the library you link your program with. Try apt-get path. – Wojtek Surowka Aug 11 '14 at 00:13
  • sudo apt-get install libboost-dev-all fixed my problem. – msknapp Aug 11 '14 at 00:16