简体   繁体   English

在C或C ++中包含标头的正确方法

[英]Proper way to include headers in C or C++

I just found out i've been doing this wrong for the whole time. 我只是发现我一直都在做错误的事情。 I haven't used any ide and only use gcc. 我没有使用过任何想法,只使用了gcc。 I have started using makefile also to compile my large project. 我已经开始使用makefile来编译我的大型项目。

most of the time the file structure was this 大多数时候文件结构是这样的

├── makefile
└── src
    ├── folder1
    │   ├── header1.cpp
    │   └── header1.h
    ├── folder2
    │   ├── header2.cpp
    │   └── header2.h
    └── main.cpp

on header2.cpp , when I include header1.h I do it like this header2.cpp ,当我包含header1.h我这样做

file header2.cpp 文件header2.cpp

#include "../folder1/header1.h"

this is how I include the other files from another folder. 这就是我包括另一个文件夹中的其他文件的方式。 I think I am doing wrong. 我想我做错了。 most of the tutorial I have watch uses Ide and they don't include it like that. 我看过的大多数教程都使用Ide,但它们并没有像这样包含它。

some include it like this 有些像这样

#include "folder1/header1.h"

or others put it in a one folder like headers/ 或其他人将其放在一个文件夹中,例如headers/

then include it like this. 然后像这样包含它。

#include "header1.h"

Can anyone guide me. 谁能指导我。 how do i achieve this. 我该如何做到这一点。 I been doing this bad including I guess. 我一直在做这坏事,包括我猜。

I don't want to include files like this 我不想包含这样的文件

 #include "../../../../sofarfolder1/header1.h"

thanks. 谢谢。 it makes me puke everytime I see my code. 每当我看到自己的代码时,我都会感到恶心。

You can use the -Idir flag to tell GCC to look for header files in the directory dir , if you don't want to use ../ . 如果您不想使用../ ,可以使用-Idir标志告诉GCC在目录dir查找头文件。

More info: https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html 更多信息: https//gcc.gnu.org/onlinedocs/cpp/Search-Path.html

https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

In your makefile, you can invoke gcc with -I../../../sofardirectory 在您的makefile中,您可以使用-I../../../sofardirectory调用gcc

That way, it will look in that directory for headers you include. 这样,它将在该目录中查找您包含的标头。

How I deal with headers depends in if they are going to be installed (as with a library) or not. 我如何处理标头取决于是否要安装标头(与库一样)。

Private headers I would keep in the project source folder: 我将保留在项目源文件夹中的专用头文件:

├── Makefile
└── src
    ├── header1.cpp
    └── header1.h
    ├── header2.cpp
    └── header2.h
    └── main.cpp

Then just include them like this: 然后像这样包含它们:

#include "header1.h"

Public headers (to be installed) I generally put in a project subfolder like this: 公共头文件(要安装)我通常将项目子文件夹放在这样的目录中:

├── Makefile
└── src
    ├── project
    │   ├── header1.h
    │   └── header2.h
    └── header1.cpp
    └── header2.cpp
    └── main.cpp

And I include them like: 我将它们包括在内:

#include <project/header1.h>

In order to locate the public headers you need to set a compiler flag. 为了找到公共头文件,您需要设置一个编译器标志。 For GCC that is -I 对于GCC -I

g++ -Isrc ... etc ...

When the headers are installed they will go somewhere like /usr/include : 安装头文件后,它们将进入/usr/include

 ── usr
    └── include
        ├── project
        │   ├── header1.h
        │   └── header2.h

And client software will include them the same way: 客户端软件将以相同方式包括它们:

#include <project/header1.h>

But they will supply different flag settings to find them: 但是它们将提供不同的标志设置来查找它们:

g++ -I/usr/include ... etc ...

An unusual alternative to using compiler flags to specify additional include directories is to use the C Preprocessor to create defined constants for the include file path. 使用编译器标志指定其他包含目录的一种不寻常的替代方法是使用C预处理器为包含文件路径创建定义的常量。

For instance if you have an include file with a relative path such as #include "../../stuff/lib1/thing1.h" you can do something like the following. 例如,如果您有一个包含文件的相对路径,例如#include "../../stuff/lib1/thing1.h" ,则可以执行以下操作。

#define THING1_H_PATH  "../../stuff/lib1/thing1.h"
  // ...
#include THING1_H_PATH

The gcc C Preprocessor documentation, The C Preprocessor in section 1.3.2, The #include Directive has this to say: gcc C预处理程序文档, 第1.3.2节“ #include指令”中的C预处理程序具有这样的含义

#include anything else #包括其他任何东西

This variant is called a computed #include. 此变量称为计算的#include。 Any `#include' directive whose argument does not fit the above two forms is a computed include. 参数不符合以上两种形式的任何“ #include”指令都是计算的包含。 The text anything else is checked for macro calls, which are expanded (see section 1.4 Macros ). 将检查其他任何文本以进行宏调用,并将其展开(请参见1.4宏部分 )。 When this is done, the result must fit one of the above two variants--in particular, the expanded text must in the end be surrounded by either quotes or angle braces. 完成此操作后,结果必须适合上述两个变体之一-特别是,扩展的文本最后必须用引号或尖括号括起来。

This feature allows you to define a macro which controls the file name to be used at a later point in the program. 此功能允许您定义一个宏,该宏控制在程序的稍后位置使用的文件名。 One application of this is to allow a site-specific configuration file for your program to specify the names of the system include files to be used. 一种应用是允许您的程序的特定于站点的配置文件指定要使用的系统包含文件的名称。 This can help in porting the program to various operating systems in which the necessary system header files are found in different places. 这有助于将程序移植到各种操作系统,在这些操作系统中,可以在不同的位置找到必要的系统头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM