C++ #include 父目录的文件

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

#include parent directory's file

c++includecmake

提问by Dipro Sen

My folder structure is

我的文件夹结构是

libA
    x.h
    y.h
    algorithm/
        a.h

Now in a.hI have #include "libA/x.h"which is not working. Its searching for algorithm/libA/x.h. So should I use #include "../x.h"? Is the second option a bad design ? currently libA is header only. but latter I may choose to compile it as a library

现在a.h我有#include "libA/x.h"哪个不起作用。它正在寻找algorithm/libA/x.h. 那么我应该使用#include "../x.h"吗?第二个选项是一个糟糕的设计吗?目前 libA 只是头文件。但后者我可能会选择将其编译为库

I am using cmake So can I or Should I add libAin my include path ?

我正在使用 cmake 那么我可以还是应该添加libA我的包含路径?

In short

简而言之

some files in My algorithm directory needs to include definitions from its parent folder. I cannot make all functions templated because the types are obvious and it will be overdoing. So How should I design My project ?

我的算法目录中的某些文件需要包含其父文件夹中的定义。我不能将所有函数都模板化,因为类型很明显,而且会做得过火。那么我应该如何设计我的项目?

回答by anatolyg

Your solution with #include "../x.h"will work. Regarding whether this is bad design - probably it is; hard to tell without knowing more about your code.

您的解决方案#include "../x.h"将起作用。关于这是否是糟糕的设计 - 可能是;如果不了解更多关于你的代码,很难说。

Consider the fact that if you have many include paths, the compiler/preprocessor will look for ../x.his all of them, which may be unintended and too broad!

考虑这样一个事实,如果您有很多包含路径,编译器/预处理器将查找../x.h所有这些路径,这可能是无意的且范围太广!

Suppose you have the following directory structure, and Your_Codeis in a search path for include-files.

假设您具有以下目录结构,并且Your_Code位于包含文件的搜索路径中。

Unrelated_Directory/
    x.h - unrelated
    Your_Code/
        libA/
            x.h - the real one
            algorithm/
                a.h

This is dangerous. If you remove/rename your real x.h, the compiler will silently pick Your_Code/../x.h, which contains unrelated stuff - this can lead to cryptic error messages. Or even worse, this may be an old version, full of bugs!

这是危险的。如果您删除/重命名您的 real x.h,编译器将默默地选择Your_Code/../x.h,其中包含不相关的内容 - 这可能会导致神秘的错误消息。或者更糟的是,这可能是旧版本,漏洞百出!

回答by anatolyg

When making a library that I know I will use in other projects, I tend to use boost's inclucision style:

在制作一个我知道我会在其他项目中使用的库时,我倾向于使用 boost 的包含风格:

#include <libA/x.h>

#include <libA/x.h>

This means that as long as the folder above "libA" (probably /include) is there, you can reference anything and everything beneath and with "libA". It also helps avoid collisions of similary-named inclusion files when you include things boost-style, because in your library and outside of your library's headers and other related code, you're always specifying the library you want to pull "x.h" from, e.g.

这意味着只要“libA”(可能/include)上方的文件夹存在,您就可以引用“libA”下方的任何内容和所有内容。当您包含 boost 样式的东西时,它还有助于避免名称相似的包含文件的冲突,因为在您的库中以及库的标题和其他相关代码之外,您总是指定要从中提取“xh”的库,例如

#include <SexyLib/x.h> // Two different x.h
#include <TheLibFarAway/x.h>  // but same name! I hope you also have Namespaces :D

This is only personal preference, but it seems to work out well for the libraries I'm developing and for boostas well. Hope that helps!

这只是个人偏好,但它似乎对我正在开发的库以及boost同样适用。希望有帮助!

回答by zacaj

If you are using gcc, you can add -IPathTo/libA to add libA to the list of folders and then use #include "x.h"

如果您使用的是 gcc,您可以添加 -IPathTo/libA 将 libA 添加到文件夹列表中,然后使用 #include "xh"