#include父目录的文件
我的文件夹结构是
libA
x.h
y.h
algorithm/
a.h
现在在 a.h
中,我有 #include"libA/x.h"
无法正常工作.其搜索 algorithm/libA/x.h
.所以我应该使用 #include"../x.h"
吗?第二种选择是不好的设计吗?目前,libA仅是标头.但后来我可能会选择将其编译为库
Now in a.h
I 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
我正在使用cmake我可以还是应该在包含路径中添加 libA
?
I am using cmake So can I or Should I add libA
in my include path ?
我的算法"目录中的某些文件需要包含其父文件夹中的定义.我不能使所有函数都模板化,因为类型很明显,而且会被滥用.那么我应该如何设计我的项目呢?
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 ?
您的带有 #include"../x.h"
的解决方案将起作用.关于这是否是不好的设计-可能是这样;在不了解您的代码的情况下很难说出来.
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.
请考虑以下事实:如果您有很多包含路径,则编译器/预处理器将查找所有 ../x.h
,这可能是意料之外的,而且范围太广了!
Consider the fact that if you have many include paths, the compiler/preprocessor will look for ../x.h
is all of them, which may be unintended and too broad!
假设您具有以下目录结构,并且 Your_Code
位于包含文件的搜索路径中.
Suppose you have the following directory structure, and Your_Code
is in a search path for include-files.
Unrelated_Directory/
x.h - unrelated
Your_Code/
libA/
x.h - the real one
algorithm/
a.h
这很危险.如果您删除/重命名真实的 x.h
,编译器将默默地选择 Your_Code/../x.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!