32

I am trying to compile a code and I get the error

undefined reference to boost::program_options::options_description::m_default_line_length

I use g++ in Ubuntu 12.04. Although I have done some C++ programming I am new to the Linux development environment (used only IDEs previously).

So I did a basic search for this trouble, and found about some linking issues. I didn't quite understand them as I am a newbie. Reading some of those solutions confused me further. My boost library folder is in /usr/include. Some solutions says that it should be in /usr/lib. But I don't have any boost folder there.

What do I need to change?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Picowhat
  • 341
  • 1
  • 3
  • 7
  • 1
    open a terminal and write `updatedb && locate libboost_program_options`. that will find the path where you installed the lib files for boost. – default Aug 29 '12 at 13:45
  • locate libboost_program_options doesnt return anything but as I said boost folder is there in /usr/include and that also has a folder program options – Picowhat Aug 29 '12 at 14:10
  • OOps sorry just saw that edit now.Ran that command and got this errorupdatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db – Picowhat Aug 29 '12 at 14:12
  • 1
    might need sudo.. you can also locate the file with the `find` command, but I find the `locate` command to be much easier to use. the `updatedb` command simply updates the database that `locate` uses – default Aug 29 '12 at 14:13
  • I have MATLAB installed.Now that locate command shows that file is in /usr/local/MATLAB/R2011a/bin/glnx86/libboost_program_options.so.1.40.0 – Picowhat Aug 29 '12 at 14:20
  • @Picowhat sudo apt install libboost-all-dev – Devymex Nov 16 '22 at 05:04

6 Answers6

40

If you have installed boost from repo just use -lboost_program_options that will suffice.
If you installed boost libraries in some other library, you need to specify that directoty by -L/path/to/lib

In CMake you may specify set(CMAKE_CXX_FLAGS "-lboost_program_options")

However with CMake you should use

FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(target ${Boost_LIBRARIES})
Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • I have installed from the repo,but the -lboost_program_options gives me an error /usr/bin/ld: cannot find -lboost_program_options – Picowhat Aug 29 '12 at 14:07
  • 1
    In CMake you should use `find_package(Boost ...` to locate Boost and use `target_link_libraries` to specify libraries; this helps when porting to other compilers / OS. – Silas Parker Aug 29 '12 at 14:08
  • 1
    well in my system just `-lboost_program_options` works. @user1633316: you can do a search for `libboost-* ` files. give that path with `-L` on note did you install program options from repo ? – Neel Basu Aug 29 '12 at 14:10
  • From repo I have installed libboost 1.46-dev,Now installing libboost 1.46-dev ALL – Picowhat Aug 29 '12 at 14:18
  • Actually boost is there in /usr/include.and I have given the L flag to that -L usr/include/boost/program_options – Picowhat Aug 29 '12 at 14:19
  • `usr/include/boost/` is where headers are. you need to specify the lib path – Neel Basu Aug 29 '12 at 14:22
  • @user1633316 you located the lib files with the `locate` command, like I showed you. that's the path that you need to include. more information is provided [here](http://www.boost.org/doc/libs/1_51_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library) – default Aug 29 '12 at 14:42
  • Yes I located it and the problem is solved.Thanks now I will have to do the same for other libraries also like uhd. – Picowhat Aug 29 '12 at 14:45
  • 1
    For OSX with MacPorts boost libraries, use -lboost_program_options-mt – Josh Petitt Jan 26 '14 at 01:52
  • It wasn't working for me, I had to add a minimum version number. `FIND_PACKAGE(Boost 1.40 COMPONENTS program_options REQUIRED)` – Archangel Mar 09 '16 at 18:12
  • 1
    Fixed this on Ubuntu 14.04 only after installing libboost1.55-all-dev – Schuh Mar 13 '16 at 12:20
5

There were changes to the <string> class in the C++11 standard which may conflict with versions of the Boost library that were compiled with a non-C++11 compiler (such as G++-4.8). Try recompiling boost or using a version of C++ compiler that was used to compile your Boost libraries.

BlazePascal
  • 391
  • 5
  • 12
  • This brought me on the right track! I needed to compile my stuff with `g++-5` instead of `g++-4.8`. It's probably not good that Ubuntu 16.04 has `g++-4.8` as default `g++` compiler while the C++ libraries it delivers are obviously compiled via `g++-5`. – Johann Hagerer Dec 14 '16 at 11:09
3

Also double check that the setting of the pre-processor variable _GLIBCXX_USE_CXX11_ABI is identical to the setting of the variable that was used for compiling boost. The default setting of the variable may be different depending on the Linux distribution and version of the GNU compiler used.

See Dual ABI for more information.

sakra
  • 62,199
  • 16
  • 168
  • 151
1

Where are the boost libraries (files ending in .so and .a)? Find those, then add this to your link command: -L/path/to/boost/libs -lname-of-boost-lib

This has to be the most common problem people face when first starting c++. There are probably a thousand other undefined reference questions on SO. Just search for undefined reference.

anio
  • 8,903
  • 7
  • 35
  • 53
1

I had the same problem and struggled for a looong time and nothing worked... but the fix was very simple.

First, execute apt install libboost-all-dev. After this is completed, executing whereis libboost_program_options should yield an output similar to

libboost_program_options: /usr/lib/x86_64-linux-gnu/libboost_program_options.so /usr/lib/x86_64-linux-gnu/libboost_program_options.a

Once this is done (and I know this is silly but) make sure, to link the libraries in the correct order, i.e. after your source files.

In my case, I was executing g++ -lboost_program_options main.cpp instead of g++ main.cpp -lboost_program_options and NOTHING worked, not a single fix I could find (and I had the exact error as mentioned in the post). That's two days in my life wasted and I truly hope somebody will stumble across this reply and save themselves some trouble.

Maurycyt
  • 676
  • 3
  • 19
0

The libraries are normally installed into /usr/lib (e.g. on my system, /usr/lib/x86_64-linux-gnu/libboost_program_options.so.1.58.0).

In order to compile code that is going to link with those libraries, you normally use the header files, provided in /usr/include.

Unlike many libraries, Boost doesn't come with pkg-config files, so you need to add the linker flags yourself. With the usual Makefile rules, you'll need something like LDLIBS += -lboost_program_options.

Note also that, although the libboost-dev package provides the headers for program_options, you need also to install libboost-program-options-dev to get the corresponding library.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103