在c ++中包含与项目目录相关的文件(Including files relative to project directory in c++)

我有一些头文件包含在项目源的子目录中。 在我看来,必须在这些文件中执行#include "../../Foo.h"看起来有点麻烦,当我更愿意执行#include "dir1/dir2/Foo.h"以加快人类阅读和理解 - 这是可能的,如果是这样,怎么样?

I have some header files contained in sub directories of the project source. Having to do #include "../../Foo.h" inside these files looks kinda messy in my opinion, when I'd much prefer to do #include "dir1/dir2/Foo.h" for faster human reading and understanding - is this possible, and if so, how?

最满意答案

显然你可以。 (而且应该)

告诉编译器将“projectdir /../ ..”添加到包含路径中。 在微软,这将使用/I开关,在gcc上它是-I 。

----编辑-----

在Visual Studio中,您可以使用“宏” 。 在这种情况下$(ProjectDir)/../..应该这样做。

更好的是,如果您可以这样组织:在“根”目录下创建解决方案,在子目录中添加项目,并在C ++包含路径中使用$(SolutionDir)/projectX/includes 。

更好的是:将依赖项目添加为项目的引用 。 VS应该自动将它的目录和lib添加到编译器和链接器设置中。

Obviously you can. (And should)

Tell the compiler to add "projectdir/../.." to it's include path. On Microsoft this would be using the /I switch, on gcc it's -I.

---- edit -----

In Visual Studio, you can use 'Macros'. In this case $(ProjectDir)/../.. should do.

Better still, if you can organize it that way: create a solution in the 'root' dir, add projects in subdirs, and use $(SolutionDir)/projectX/includes on your C++ include path.

Even more better still: add the dependent project as a Reference to your project. VS should automatically add it's directory and lib to the compiler and linker settings.

更多推荐

Foo,#include,files,电脑培训,计算机培训,IT培训"/> <meta name="descriptio