C++文件系统上的组织
我来自 Java/AS3/Javascript 背景,我的所有类都被组织到有助于表示其功能的包中。
在启动 C++ 项目时,我试图以几乎相同的方式模仿此文件系统结构,但我遇到了包含问题。
目前我有一个 src 目录,其中包含 main.cpp 文件。然后我有一些根目录和里面的其他文件。下面是一个示例:
src
->main.cpp
->window
---->Window.h
---->Window.cpp
main.cpp 包含 Window.h 并使用语句 #include "Window.h"一切都构建得很好。但如果我重新启动 Visual Studio,它会抱怨找不到“Window.h”。
在寻找开源项目时,我看到一些项目只是将所有源文件放在一个目录中,没有嵌套,以便轻松包含,我想。有些将头文件和 cpp 文件分开。
在文件系统上组织大型 C++ 项目的正确方法(或者至少是一种不会引起麻烦的方法)是什么?
谢谢!
I come from a Java/AS3/Javascript background where all of my classes are organized into packages that help denote their functionality.
In starting a C++ project I sought to mimic this file system structure in mostly the same way but I've been running into issues with the includes.
Currently I have an src directory with the main.cpp file inside it. Then I have some root directories and with other files inside. Here's an example:
src
->main.cpp
->window
---->Window.h
---->Window.cpp
main.cpp includes Window.h with the statement #include "Window.h" and everything builds just fine. But if i restart Visual Studio, it complains that it can't find "Window.h".
In looking a open source projects, I've seen some that just have all the source files in one directory with no nesting for easy includes I suppose. Some have the headers and cpp files separated.
What is the correct way (or at least a way that will cause less headaches) to organize a large-ish C++ project on the file system?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像您尝试做的那样打破它是合理的并且很容易完成。
您只需要设置项目的包含路径。在 Visual Studio 中,右键单击项目名称,然后单击“属性”。从那里,在左侧的树控件中,展开“C/C++”,然后在树中选择“常规”。右侧的第一个选项应该是“其他包含目录”。
您有几个选项:
您可以指定特定的包含目录(用分号分隔)。例如,如果您有文件夹“Window”和“Printing”,您可以输入:
..\Window;..\Printing
这将允许您轻松包含窗口中的文件并进行打印,例如this:
上述方法有一些缺点,因为您很容易与您可能正在使用的其他库中的名称发生冲突,这使得包含顺序非常重要。
更好的方法(在我看来)是将以下内容添加为包含路径:
..\
这将使其在查找包含时搜索父目录。这使您可以在包含路径中更加详细,如下所示:
Breaking it out like you've tried to do is reasonable and easy enough to accomplish.
You just need to set your project's include paths. From Visual Studio, right click on the project name and click "Properties". From there, in the tree control on the left hand side, expand "C/C++", and then select "General" in the tree. The first option on the right hand side should then be "Additional Include Directories".
There you have several options:
You can specify specific include directories (separated by semicolons). For instance, if you had folders "Window" and "Printing" you could put in:
..\Window;..\Printing
Which would allow you to include the files from window and printing easily, like this:
The above approach has some drawbacks, as you can easily collide with names from other libraries you may be using, making the include ORDER very important.
A better approach (in my opinion) is to add the following as an include path:
..\
This will make it search the parent directory when looking for includes. This allows you to be more verbose in your include paths, like this:
遵循 Java 示例并按 C++ 命名空间排列源文件是有意义的。在
/src
目录下创建与命名空间相对应的子文件夹。It makes sense to follow the Java example and arrange source files by C++ namespace. Create sub-folders under your
/src
directory that correspond to the namespaces.