如何将 boost 库包含到 C++ 程序中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17184857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:59:22  来源:igfitidea点击:

How to include the boost library into a C++ program?

c++boostcompiler-constructioninclude

提问by Roman

I am trying to compile this small program:

我正在尝试编译这个小程序:

#include <boost/math/distributions/poisson.hpp>

namespace boost { namespace math {

template <class RealType = double, 
          class Policy   = policies::policy<> >
class poisson_distribution;

typedef poisson_distribution<> poisson;

template <class RealType, class Policy>
class poisson_distribution
{ 
public:
  typedef RealType value_type;
  typedef Policy   policy_type;

  poisson_distribution(RealType mean = 1); // Constructor.
  RealType mean()const; // Accessor.
}

}} // namespaces boost::math

This code is taken from here.

此代码取自此处

The compiler tells me that boost/math/distributions/poisson.hppis not found. So, I try to find this file by myself (using locate poisson.hppcommand). I find the following file: /opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/boost/math/distributions/poisson.hpp. So, in my code I put the full name of the file to make sure that compiler finds it:

编译器告诉我boost/math/distributions/poisson.hpp没有找到。所以,我尝试自己找到这个文件(使用locate poisson.hpp命令)。我找到以下文件:/opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/boost/math/distributions/poisson.hpp. 因此,在我的代码中,我输入了文件的全名以确保编译器找到它:

#include </opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/boost/math/distributions/poisson.hpp>

But now I get another error message: boost/math/distributions/fwd.hppis not found.

但现在我收到另一条错误消息:boost/math/distributions/fwd.hpp未找到。

Is there a way to force the compiler to search the files in the correct directory?

有没有办法强制编译器搜索正确目录中的文件?

I use g++compiler.

我使用g++编译器。

回答by Rody Oldenhuis

You need an include path in your g++ command:

您需要在 g++ 命令中包含路径:

g++ -I/opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/  [rest of command here]

(and possibly a link to a library path as well).

(也可能是指向库路径的链接)。

In general, it's nota good idea to put full paths in your source code; that kind of completely destroys the idea of portability :) (meaning, that code can no longer be compiled on any other PC in the world than your own, and even thatis going to be dubious half a year from now).

通常,在源代码中放置完整路径不是一个好主意;那种完全破坏便携性的想法:)(意思是,代码不再被任何其他个人电脑在世界上编译比你自己,甚至将是一年半可疑从现在起)。

Anyway, if you find yourself typing long compiler lines like the one above, it's really time to start using a makefile.

无论如何,如果您发现自己输入了像上面这样的长编译器行,那么现在是开始使用makefile 的时候了

You'll probably find this questioninteresting as well.

你可能也会发现这个问题很有趣。

回答by Yehezkel B.

(This isn't a direct answer to the question, but a list of considerations that I think should be addressed with the final and complete answer that @uo???P?z??C wants to see here.)

(这不是对问题的直接回答,而是我认为应该在@uo???P?z??C 想在这里看到的最终和完整答案中解决的一系列考虑因素。)

The question of handling 3rd party dependencies with C++ isn't a simple one. There are many approaches for this and choosing the approach that is right for you depends on your toolset and environment, on your project management and on the trade-offs you want to take.

使用 C++ 处理 3rd 方依赖项的问题并不简单。对此有很多方法,选择适合您的方法取决于您的工具集和环境、您的项目管理以及您想要进行的权衡。

For Boost, we have to remember that it's mostly header-only library, but some components include a separately-compiled part too (can be static or dynamic lib, can be mandatory for the component or just for a specific use-case of it). E.g. Boost.Filesystem requires compilation, Boost.Graph requires it only if you want to parse GraphViz files and Boost.Variant doesn't need it at all (is "header-only" library). For details, see http://www.boost.org/doc/libs/release/more/getting_started/unix-variants.html#header-only-libraries(this redirects to the latest version, which is currently 1.61).

对于 Boost,我们必须记住它主要是仅头文件的库,但有些组件也包含单独编译的部分(可以是静态或动态库,可以是组件必需的,也可以只是它的特定用例) . 例如 Boost.Filesystem 需要编译,Boost.Graph 仅在您想解析 GraphViz 文件时才需要它,而 Boost.Variant 根本不需要它(是“仅标头”库)。有关详细信息,请参阅http://www.boost.org/doc/libs/release/more/getting_started/unix-variants.html#header-only-libraries(此重定向到最新版本,目前为 1.61)。

Using only header-only parts of Boost simplifies many of the considerations, but, of course, sometimes you need the other parts too.

仅使用 Boost 的仅标题部分可以简化许多考虑事项,但是,当然,有时您也需要其他部分。

Things to consider:

需要考虑的事项:

  1. Are you using only header-only parts of Boost or you need a separately-compiled part too? If you need a separately-compiled part, do you use static lib or dynamic lib? If you use separately-compiled part and want to use dynamic lib, you have to decide how to find the dynamic lib when running the application (especially if you distribute your project as binary).
  2. Is your project distributed as source or as binary? In the binary case, you worry mainly about the developer workflow (but see the point above about using dynamic lib). In the source case, you want it to be easy to compile on every other computer.
  3. Do you want your project to stick with the same version of Boost (at least until you explicitly decide to change version) or you want it to use whatever is installed on the specific machine (assuming there is no API changes)?
  4. Are you OK with having a copy of Boost (or part of it) with the project or you want a central location for all your projects to use?
  5. How much manual config steps you want to force on the users (end users or developers, depends on the other questions above)? (0 is probably preferred, but there is always a trade-off.)
  6. Is your project Windows-only, Linux-only, etc.? Each platform has its own ways and depending on your answer to other questions, the methods you should use can vary between OSes. Cross-platform, for our topic, usually means that you have to implement the relevant approaches for each of the platforms you want to support.
  7. What is your toolset and build environment (e.g. Visual Studio, Qt, make, simple scripts, etc.)?
  1. 您是否仅使用 Boost 的仅标头部分,或者您也需要单独编译的部分?如果需要单独编译的部分,是使用静态库还是动态库?如果您使用单独编译的部分并希望使用动态库,则必须在运行应用程序时决定如何找到动态库(尤其是当您将项目作为二进制分发时)。
  2. 您的项目是作为源还是二进制分发?在二进制情况下,您主要担心开发人员的工作流程(但请参阅上面关于使用动态库的要点)。在源代码的情况下,您希望在其他所有计算机上都可以轻松编译。
  3. 您希望您的项目坚持使用相同版本的 Boost(至少在您明确决定更改版本之前)还是希望它使用特定机器上安装的任何内容(假设没有 API 更改)?
  4. 您是否同意在项目中使用 Boost(或其中的一部分)的副本,或者您想要一个中央位置供所有项目使用?
  5. 您想对用户(最终用户或开发人员,取决于上述其他问题)强制执行多少手动配置步骤?(0 可能是首选,但总有一个权衡。)
  6. 您的项目是否仅适用于 Windows、仅适用于 Linux 等?每个平台都有自己的方法,根据您对其他问题的回答,您应该使用的方法可能因操作系统而异。对于我们的主题,跨平台通常意味着您必须为要支持的每个平台实施相关方法。
  7. 您的工具集和构建环境是什么(例如 Visual Studio、Qt、make、简单脚本等)?

回答by Szabolcs Dombi

To specify a directory to search for include files:

要指定搜索包含文件的目录:

-I /opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include

To specify a directory to search for libraries:

要指定一个目录来搜索库:

-L /opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/lib

To specify the actual library name:

要指定实际的库名称:

-l foo

when your library is called libfoo.a

当你的库被称为 lib foo.a

you don't have to write space after -I, -Lor -lfor now it is more readable.

您不必在 之后写空间-I-L或者-l现在它更具可读性。

HINT:

暗示:

Use Makefile. Maybe you have the boost include patch already exported to some environment variable.

使用生成文件。也许您已经将 boost include 补丁导出到某个环境变量。