如何在 C++ windows 中包含第三方库?

How to include third party libraries in C++ windows?

我的下载文件夹中有 boost 库。当我尝试包含特定文件时。它正在抛出错误。下面是代码和我做的步骤。

\main.cpp

#include "type_index.hpp"

int main(){
//some code
return 0;
}

我打开命令提示符并运行以下命令

g++ -IC:\Users\Owner\Downloads\boost_1_70_0\boost -o main main.cpp

我在命令提示符中收到以下错误

In file included from main.cpp:2:0:
C:\Users\Owner\Downloads\boost_1_70_0\boost/type_index.hpp:17:28: fatal error: boost/config.hpp: No such file or directory
 #include <boost/config.hpp>
                            ^
compilation terminated.

如何运行上述文件?我是否必须将 boost 目录的位置从下载文件夹更改为 mingw 目录中的某个位置?

添加目录图片:

假设 boost 已在您的系统上正确配置和构建,将会有一个位置,其中包含 root 的 boost 集线器所在的位置。例如:如果您在 c:\Stuff\boost_1_70_0 中下载并构建了 boost,那么该文件夹中将是 boost 包含集 c:\Stuff\boost_1_70_0\boost 的中心,它包含所有 boost header。

通过修改包含路径来引用boost,以提供对boost include hub的访问; 提供对 top-most headers in 中心的访问。与 openssl 类似,boost 的所有 header 都包含在它们的 own header 中,其中 boost/。 boost 的消费者也应该这样做,因此,包含路径必须包含可以找到 boost/ hub 的文件夹。它应该包括boost/集线器本身作为路径的一部分。

例如:这是正确的

g++ -Ic:\Stuff\boost_1_70_0 -o main main.cpp

另一方面,这是错误的:

g++ -Ic:\Stuff\boost_1_70_0\boost -o main main.cpp

对于前者,当代码包括:

#include <boost/asio.hpp>

搜索包含路径,找到文件。此外,在 header 中,当编译器看到此内容时:

#include <boost/asio/associated_allocator.hpp>

它仍然可以正确解析,因为将 "thing" 放在包含路径中的一个文件夹的末尾是有效的。

现在,考虑错误的情况。如果您将包含路径配置为不小心指定了 boost/root hub 本身,会发生什么情况?好了,现在 可以做到了:

#include <asio.hpp>

但是一旦预处理器在 header 上启动,它将看到:

#include <boost/asio/associated_allocator.hpp>

嗯..糟糕。 pre-processor 会寻找它,但永远找不到它

摘要

在您的源代码中使用 boost headers 时,您总是使用 boost hub 序言引用它们:

#include <boost/headername.hpp>

并且始终将 boost/ 集线器所在的文件夹包含在您的构建配置中作为修改后的包含路径; 不是 包含 boost/ 集线器的完整路径。