如何在 C++ 项目中包含 boost::thread?

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

How to include boost::thread in your C++ project?

c++boostmultithreading

提问by Chris

What do I need to do to include boost::thread in my project? I have copied the whole thread folder to my working path (I wish to be able to run this on several computers) and I get

我需要做什么才能在我的项目中包含 boost::thread?我已将整个线程文件夹复制到我的工作路径(我希望能够在多台计算机上运行它),然后我得到

fatal error C1083: Cannot open include file: 'boost/thread/detail/platform.hpp': No such file or directory

致命错误 C1083:无法打开包含文件:“boost/thread/detail/platform.hpp”:没有这样的文件或目录

From the line #include "thread/thread.hpp"

从线 #include "thread/thread.hpp"

What gives?

是什么赋予了?

edit:Even if I just link to the boost folder where the precompiled binary installed and I use #include <boost/thread/thread.hpp>I get

编辑:即使我只是链接到安装了预编译二进制文件的 boost 文件夹,#include <boost/thread/thread.hpp>我也得到了

fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-1_41.lib'

致命错误 LNK1104:无法打开文件“libboost_thread-vc90-mt-1_41.lib”

回答by Kornel Kisielewicz

Unfortunately boost::thread is not a "header-only" library -- hence you need to have it compiled. There are basically two ways to go around it.

不幸的是 boost::thread 不是“仅标头”库——因此您需要对其进行编译。基本上有两种方法可以绕过它。

  1. you download a prebuilt install package from boostpro (assuming that you are on windows) -- https://sourceforge.net/projects/boost/files/boost-binaries/
  2. you can build it yourself - see http://www.boost.org/doc/libs/1_35_0/more/getting_started/index.html
  1. 你下载一个预建从boostpro安装包(假设你是在Windows上) - https://sourceforge.net/projects/boost/files/boost-binaries/
  2. 您可以自己构建它 - 请参阅http://www.boost.org/doc/libs/1_35_0/more/getting_started/index.html

回答by AndyUK

Once you have downloaded, unzipped and installed the boost libraries in your Visual Studio environment, and told the Visual Studio project where the Boost libraries live, you are not quite finished yet. There exist a number of libraries in Boost libraries that require that you build them yourself. Boost threads is one such library.

在您的 Visual Studio 环境中下载、解压并安装 Boost 库并告诉 Visual Studio 项目 Boost 库所在的位置后,您还没有完全完成。Boost 库中存在许多需要您自己构建的库。Boost 线程就是这样一个库。

  1. Build the bjam.exe program if you have not already done it. Probably the simplest way to is to get and run it direct from BoostPro, telling the installation which of the libraries (threads) you wish to install – you don't have to install all of them.

  2. Go to the C:\Program Files\boost_1_46_1\tools\build\v2\engine\src directory and run build.bat from the command prompt. Running the build.bat script will create bjam.exe inside this directory: C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86

  3. Select the bjam.exe into in your PATH environment variables. Include the directory C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86 as another environment variable.

  4. At the command prompt, go to the C:\Program Files\boost_1_46_1 directory, enter “bjam”, waiting for approximately 5-10 minutes while the program gets created.

  5. In your Visual Studio project select Configuration Properties -> Linker -> Input -> Additional Dependencies and enter libboost_thread-vc100-mt-gd-1_46_1.lib.

  6. In your Visual Studio project set the project configuration properties -> Linker -> General -> Additional Include Directories, telling it the location of the stage/lib folder eg C:\Program Files\Boost_1_46_1\stage\lib.

  1. 如果您还没有构建 bjam.exe 程序。可能最简单的方法是直接从BoostPro获取并运行它,告诉安装程序您希望安装哪些库(线程)——您不必安装所有这些库(线程)。

  2. 转到 C:\Program Files\boost_1_46_1\tools\build\v2\engine\src 目录并从命令提示符运行 build.bat。运行 build.bat 脚本将在此目录中创建 bjam.exe:C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86

  3. 在 PATH 环境变量中选择 bjam.exe。包括目录 C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86 作为另一个环境变量。

  4. 在命令提示符下,转到 C:\Program Files\boost_1_46_1 目录,输入“bjam”,等待大约 5-10 分钟,程序创建完成。

  5. 在您的 Visual Studio 项目中,选择 Configuration Properties -> Linker -> Input -> Additional Dependencies 并输入 libboost_thread-vc100-mt-gd-1_46_1.lib。

  6. 在您的 Visual Studio 项目中,设置项目配置属性 -> 链接器 -> 常规 -> 附加包含目录,告诉它 stage/lib 文件夹的位置,例如 C:\Program Files\Boost_1_46_1\stage\lib。

That should be sufficient to get you going. For more comprehensive details, please see this blog posting.

这应该足以让你前进。有关更全面的详细信息,请参阅此博客文章

回答by Pabitra Dash

I was getting compile time error for 'boost::thread'. But it is resolved when I included following header.

我收到“boost::thread”的编译时错误。但是当我包含以下标题时它就解决了。

#include <boost\thread.hpp>

回答by anno

Fatal Error C1083 is a Visual C++ error. You should include the library folder from boost in your project. "C:\Program Files\boost\boost_1_41\lib" if you're using boostpro.

致命错误 C1083 是 Visual C++ 错误。您应该在项目中包含来自 boost 的库文件夹。“C:\Program Files\boost\boost_1_41\lib”(如果您使用的是 boostpro)。

Also when you're downloading the thread library with boostpro, you need to check it in the list (you can also choose the compiler...).

此外,当您使用 boostpro 下载线程库时,您需要在列表中进行检查(您也可以选择编译器...)。