C++ 对 `boost::program_options::options_description::m_default_line_length' 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12179154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined reference to `boost::program_options::options_description::m_default_line_length'
提问by Picowhat
I am trying to compile a code and I get the error
我正在尝试编译代码,但出现错误
undefined reference to
boost::program_options::options_description::m_default_line_length
未定义的引用
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).
我在 Ubuntu 12.04 中使用 g++。尽管我已经完成了一些 C++ 编程,但我对 Linux 开发环境还是很陌生(以前只使用过 IDE)。
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.
所以我对这个问题做了一个基本的搜索,发现了一些链接问题。我不是很懂他们,因为我是新手。阅读其中一些解决方案让我更加困惑。我的 boost 库文件夹在/usr/include
. 一些解决方案说它应该在/usr/lib
. 但我那里没有任何 boost 文件夹。
What do I need to change?
我需要改变什么?
回答by Neel Basu
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
如果您已经从 repo 安装了 boost,只需使用它就-lboost_program_options
足够了。
如果您在其他某个库中安装了 boost 库,则需要通过以下方式指定该目录-L/path/to/lib
In CMake you may specify set(CMAKE_CXX_FLAGS "-lboost_program_options")
在 CMake 中,您可以指定 set(CMAKE_CXX_FLAGS "-lboost_program_options")
However with CMake you should use
但是,对于 CMake,您应该使用
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(target ${Boost_LIBRARIES})
回答by BlazePascal
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.
<string>
C++11 标准中对类的更改可能与使用非 C++11 编译器(例如 G++-4.8)编译的 Boost 库版本冲突。尝试重新编译 boost 或使用用于编译 Boost 库的 C++ 编译器版本。
回答by sakra
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.
还要仔细检查预处理器变量_GLIBCXX_USE_CXX11_ABI
的设置是否与用于编译 boost 的变量设置相同。根据 Linux 发行版和所用 GNU 编译器的版本,变量的默认设置可能会有所不同。
See Dual ABIfor more information.
有关详细信息,请参阅双 ABI。
回答by anio
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
boost 库(以 .so 和 .a 结尾的文件)在哪里?找到这些,然后将其添加到您的链接命令中:-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.
这一定是人们第一次开始 C++ 时面临的最常见的问题。关于 SO 可能还有一千个其他未定义的参考问题。只需搜索未定义的引用。
回答by Toby Speight
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
).
这些库通常安装在/usr/lib
(例如在我的系统上/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
.
为了编译将与这些库链接的代码,您通常使用/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
.
与许多库不同,Boost 不附带 pkg-config 文件,因此您需要自己添加链接器标志。使用通常的 Makefile 规则,您将需要类似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.
另请注意,虽然该libboost-dev
包提供了 的头文件program_options
,但您还需要安装libboost-program-options-dev
以获取相应的库。