C++ 包括来自不同目录的头文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8621507/
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
including header files from different directories?
提问by Stas Jaro
I am working on a project and I keep getting stumped on how I am supposed to import files from a different directory. Here is how some of my files are organized:
我正在处理一个项目,但我一直对如何从不同目录导入文件感到困惑。以下是我的一些文件的组织方式:
-stdafx.h
-core/
-->renderer.cpp
-shapes/
-->sphere.h
-->sphere.cpp
how can i access the stdafx.h
and shapes/sphere.h
from the core/renderer.cpp
?
我如何可以访问stdafx.h
和shapes/sphere.h
从core/renderer.cpp
?
采纳答案by Michael Krelin - hacker
There are many ways. You can #include "../stdafx.h"
, for instance. More common is to add the root of your project to the include path and use #include "shapes/sphere.h"
. Or have a separate directory with headers in include path.
有很多方法。#include "../stdafx.h"
例如,你可以。更常见的是将项目的根添加到包含路径并使用#include "shapes/sphere.h"
. 或者在包含路径中有一个带有标题的单独目录。
回答by Pushpak Sharma
One (bad) way to do this is to include a relative path to the header file you want to include as part of the #include line. For example:
一种(不好的)方法是在#include 行中包含要包含的头文件的相对路径。例如:
#include "headers/myHeader.h"
#include "../moreHeaders/myOtherHeader.h"
The downside of this approach is that it requires you to reflect your directory structure in your code. If you ever update your directory structure, your code won't work any more.
这种方法的缺点是它要求您在代码中反映您的目录结构。如果您更新了目录结构,您的代码将不再起作用。
A better method is to tell your compiler or IDE that you have a bunch of header files in some other location, so that it will look there when it can't find them in the current directory. This can generally be done by setting an “include path” or “search directory” in your IDE project settings.
更好的方法是告诉您的编译器或 IDE 您在其他位置有一堆头文件,以便在当前目录中找不到它们时会查找这些头文件。这通常可以通过在 IDE 项目设置中设置“包含路径”或“搜索目录”来完成。
For Visual Studio, you can right click on your project in the Solution Explorer, and choose “Properties”, then the “VC++ Directories” tab. From here, you will see a line called “Include Directories”. Add your include directories there.
对于Visual Studio,您可以在解决方案资源管理器中右键单击您的项目,然后选择“属性”,然后选择“VC++ 目录”选项卡。从这里,您将看到一行名为“包含目录”的行。在那里添加包含目录。
For Code::Blocks, go to the Project menu and select “Build Options”, then the “Search directories” tab. Add your include directories there.
对于Code::Blocks,转到“项目”菜单并选择“构建选项”,然后选择“搜索目录”选项卡。在那里添加包含目录。
For g++, you can use the -I
option to specify an alternate include directory.
对于g++,您可以使用该-I
选项来指定备用包含目录。
g++ -o main -I /source/includes main.cpp
The nice thing about this approach is that if you ever change your directory structure, you only have to change a single compiler or IDE setting instead of every code file.
这种方法的好处在于,如果您更改了目录结构,您只需更改一个编译器或 IDE 设置,而不是每个代码文件。
回答by Dave Rager
You can either use relative paths:
您可以使用相对路径:
#include "../stdafx.h"
#include "../shapes/sphere.h"
or add your project directory to your compiler include path and reference them like normal:
或者将您的项目目录添加到您的编译器包含路径并像平常一样引用它们:
#include "stdafx.h"
#include "shapes/sphere.h"
You can use the /I
command line optionto add the path or set the path in your project settings.
您可以使用/I
命令行选项在项目设置中添加路径或设置路径。