C++ #include 头文件在不同的目录结构中时

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

#include Header Files When They Are In a Different Directory Structure

c++directoryheader-files

提问by user1205577

I have done some searching and found similar threads on the proper way to include header files in C++, but none of them quite answered this question.

我已经进行了一些搜索,并在 C++ 中包含头文件的正确方法上找到了类似的线程,但没有一个能很好地回答这个问题。

I know that in order to include a header file in another folder you can simply use the following syntax:

我知道为了将头文件包含在另一个文件夹中,您只需使用以下语法:

#include "../myFolder/myFile.h"

But what about when the file is in a different directory structure a bit far removed? For example, if something like the following is guaranteed to be true:

但是当文件在一个不同的目录结构中有点远时呢?例如,如果保证以下内容为真:

Current directory = / f1 / f2 / f3 / f4 / f5 / myFile.cpp

当前目录=/f1/f2/f3/f4/f5/myFile.cpp

Desired header directory = / f1 / d2 / d3 / d4 / d5 / d6 / myHeader.h

所需的头目录=/f1/d2/d3/d4/d5/d6/myHeader.h

I know that you can set the "Additional Include Directories" property or use a make file, but I'd like to know if there is a way to do it from the #includestatement.

我知道您可以设置“附加包含目录”属性或使用 make 文件,但我想知道是否有办法从#include语句中做到这一点。

回答by shoosh

putting ".."in #includeis generally considered ugly and unmaintainable.

".."#include通常被认为是丑陋,难以维护。

Every coherent library you're using (for instance boost) has a single root of header files hierarchy that you should put in your "additional include directories" property. For boost it is something like

您使用的每个连贯库(例如 boost)都有一个头文件层次结构的根,您应该将其放入“附加包含目录”属性中。为了提升它就像

`C:/lib/boost_1_49`

under this directory you usually find a directory called boostwhere all of the headers live. This brings the convention that all boost headers start with:

在此目录下,您通常会找到一个名为boost所有标题所在的目录。这带来了所有boost标头开头的约定:

#include <boost/bla/bla.hpp>

This also goes for the very project you're writing. You should decide what's the best root for its headers and start all the includes from there.
The only exception to this rule should be headers that are in the same directory. Those can just be included as just a file name #include "same-dir-header.h"

这也适用于您正在编写的项目。您应该决定其标题的最佳根是什么,并从那里开始所有包含。
此规则的唯一例外应该是位于同一目录中的标头。那些可以仅作为文件名包含在内#include "same-dir-header.h"

You should also make the difference between including with ""and <>. Quotes should be things in your project, angle brackets should be external libraries (or as some would have it - OS and C runtime libraries)

您还应该区分包含 with""<>。引号应该是你项目中的东西,尖括号应该是外部库(或者像有些人那样 - OS 和 C 运行时库)

回答by kebs

To complete the answer from @shoosh, you are supposed to tell your compiler where are those "other" header files. With gcc on windows, if they are in c:\path\to\library, then add the -I option

要完成@shoosh 的回答,您应该告诉编译器那些“其他”头文件在哪里。在 windows 上使用 gcc,如果它们在 c:\path\to\library 中,则添加 -I 选项

-Ic:\path\to\library

Beware of spaces in path, if the location is c:\my path\to\library, then:

注意路径中的空格,如果位置是c:\my path\to\library,则:

-I"c:\my path\to\library"

Other compilers will provide a similar option, on command line or through the IDE.

其他编译器将在命令行或通过 IDE 提供类似的选项。