C++ 如何在其他src文件夹中包含头文件

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

how to include header files in other src folder

c++compilation

提问by Richard

I have a c++ project having two src folders. Source file in folder 1 may need to include header file in src folder 2. Is it possible? or how should I write my Makefiles? thanks

我有一个包含两个 src 文件夹的 C++ 项目。文件夹 1 中的源文件可能需要在 src 文件夹 2 中包含头文件。这可能吗?或者我应该如何编写我的 Makefile?谢谢

回答by Mike Caron

Depending on how closely the two folders are related (eg, if they're the same project), then it can be as easy as:

根据两个文件夹的关联程度(例如,如果它们是同一个项目),那么它可以很简单:

#include "../otherfolder/header.h"

If they're separate projects, then it's customary to simply add the other project's header directory to your project's header search path, and include the header like this:

如果它们是单独的项目,那么通常只需将另一个项目的头目录添加到您项目的头搜索路径中,并像这样包含头:

#include <header.h>

(In practice, the brackets/quotes don't matter, but it helps keep external vs. internal header imports separate)

(实际上,括号/引号无关紧要,但它有助于保持外部和内部头文件导入分开)

回答by Mircea Ispas

Considering you have src1 and src2 folders in same folder. You have 2 solutions for this:

考虑到您在同一文件夹中有 src1 和 src2 文件夹。您有 2 个解决方案:

1 - #include "../src2/header.h"

1 - #include "../src2/header.h"

2 - Add in your project at additional include directories src2 and use normal #include

2 - 在您的项目中添加额外的包含目录 src2 并使用普通的 #include