为什么要在C和C ++项目中创建一个include/目录? [英] Why create an include/ directory in C and C++ projects?

查看:43
本文介绍了为什么要在C和C ++项目中创建一个include/目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我处理个人C和C ++项目时,通常将 file.h file.cpp 放在同一目录中,然后将 file.cpp 可以使用 #include"file.h" 指令引用 file.h .

When I work on my personal C and C++ projects I usually put file.h and file.cpp in the same directory and then file.cpp can reference file.h with a #include "file.h" directive.

但是,通常会找出所有 .h 文件都放在 include/内的库和其他类型的项目(例如linux内核和freeRTOS).目录,而 .cpp 文件保留在另一个目录中.在这些项目中, .h 文件也包含在 #include"file.h" 中,而不是 #include"include/file.h" 就像我希望的那样.

However, it is common to find out libraries and other kinds of projects (like the linux kernel and freeRTOS) where all .h files are placed inside an include/ directory, while .cpp files remain in another directory. In those projects, .h files are also included with #include "file.h" instead of #include "include/file.h" as I was hoping.

我对所有这些都有疑问:

I have some questions about all of this:

  1. 这种文件结构组织的优势是什么?
  2. 为什么在 include/中的 .h 文件包含在 #include"file.h" 而不是 #include"include/file.h"?我知道真正的把戏是在某些Makefile中,但是这样做真的好过了吗,而不是(在代码中)清楚地表明我们要包含的文件实际上在 include/目录中?/li>
  1. What are the advantages of this file structure organization?
  2. Why are .h files inside include/ included with #include "file.h" instead of #include "include/file.h"? I know the real trick is inside some Makefile, but is it really better to do that way instead of making clear (in code) that the file we want to include is actually in the include/ directory?

推荐答案

这样做的主要原因是,编译后的库需要标头才能被最终用户使用.按照惯例, include 目录的内容是公开显示的标题,以供公众使用.源目录可能具有供内部使用的标头,但这些标头不是 ,而是与已编译的库一起分发的.

The main reason to do this is that compiled libraries need headers in order to be consumed by the eventual user. By convention, the contents of the include directory are the headers exposed for public consumption. The source directory may have headers for internal use, but those are not meant to be distributed with the compiled library.

因此,在使用库时,您链接到二进制文件,并将库的 include 目录添加到构建系统的头路径中.同样,如果将已编译的库安装到集中位置,则可以告诉您需要将哪些文件复制到中心位置(已编译的二进制文件和 include 目录),而不需要将哪些文件复制(位于源目录等).

So when using the library, you link to the binary and add the library's include directory to your build system's header paths. Similarly, if you install your compiled library to a centralized location, you can tell which files need to be copied to the central location (the compiled binaries and the include directory) and which files don't (the source directory and so forth).

这篇关于为什么要在C和C ++项目中创建一个include/目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆