问题
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?
回答1:
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})
回答2:
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.
回答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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/12179154/undefined-reference-to-boostprogram-optionsoptions-descriptionm-default-l