C++ 包含完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22738979/
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
C++ include with full path
提问by valentin
I find #include "../app/thing.h"
very ugly and I would like to be able to import from the main root of my project, like this:
我觉得#include "../app/thing.h"
很丑,我希望能够从我项目的主根目录导入,如下所示:
#include <project/app/submodule/thing.h>
#include <project/app/submodule/thing.h>
(I know <> is generally used for external but I find it very cleaner)
(我知道 <> 通常用于外部,但我觉得它非常干净)
How can I do that, from anywhere in my project?
我怎么能在我的项目的任何地方做到这一点?
采纳答案by Jonathan Leffler
You simply need to ensure that your build process sets an option to specify the starting point of the search.
您只需要确保您的构建过程设置了一个选项来指定搜索的起点。
For example, if your header is in:
例如,如果您的标题位于:
/work/username/src/project/app/submodule/thing.h
then you need to include (assuming a POSIX-compliant compiler; AFAICR, even MSVC uses /I
as the analogue of -I
):
那么你需要包括(假设一个符合 POSIX 的编译器;AFAICR,甚至 MSVC/I
用作模拟-I
):
-I/work/username/src
as one of the compiler options. You can use that path from anywhere in your project since it is absolute. You just need a defined way for your build system to know what the setting should be, so that when it is moved to /home/someone/src/
, you have only one setting to change.
作为编译器选项之一。您可以在项目的任何位置使用该路径,因为它是绝对路径。您只需要一种定义的方式让您的构建系统知道应该是什么设置,这样当它移动到 时/home/someone/src/
,您只需更改一个设置。
回答by Gurgadurgen
See this answerfor a more complete explanation about how the differences between the two formats work. Honestly, though, I think you might want to consider restructuring your folder hierarchy if you need to jump up a folder then jump into another folder to get something. Generally speaking, it's pretty common practice to keep all files local to your program local to each other in the folder structure (i.e. in the same folder), and all files that aren't local, but may be needed (such as header files for libraries used) in a sub-folder within the main program folder, or to include them at compile time.
有关两种格式之间的差异如何工作的更完整说明,请参阅此答案。不过,老实说,如果您需要跳转一个文件夹然后跳转到另一个文件夹以获取某些内容,我认为您可能需要考虑重组文件夹层次结构。一般来说,将程序本地的所有文件在文件夹结构中(即在同一个文件夹中)以及所有非本地但可能需要的文件(例如用于使用的库)在主程序文件夹内的子文件夹中,或在编译时包含它们。
It is important to note that in the answer I linked above, it explains that "<>" includes are IMPLEMENTATION DEPENDENT, so we'd really need to know what compiler you're using to tell you if you could or couldn't do that.
重要的是要注意,在我上面链接的答案中,它解释了“<>”包含依赖于实现,所以我们真的需要知道你正在使用什么编译器来告诉你你是否可以做或不能做那。
回答by π?ντα ?ε?
You can simply use the include directories option of your current compiler (-I
usually) to achieve this.
您可以简单地使用当前编译器的包含目录选项(-I
通常)来实现这一点。
Also note using the ""
double quotes will just add to fallback for the compiler standard headers. Files included using the <>
, are only guaranteed to search files from the compiler standard headers.
另请注意,使用""
双引号只会增加编译器标准头文件的回退。使用<>
,包含的文件只能保证从编译器标准头文件中搜索文件。