Are there any articles or recommendations how to organize file hierarchy in your project? I am interested in how to name folders, to separate sources and headers or not.
I开发者_如何学JAVA have project written in C++, a library and project using it. Library has many components, they are separated from each other but some of them use common files. Should I create directories for them?
I will be glad to hear all recommendations.
Do not split headers and source files into separate folders. It does nothing more than add an extra folder level.
At best it is completely useless; if you're looking for "widget.h" you can trivially find it even if there's a "widget.cpp" right next to it. At worst it's rather counter-productive - e.g. when you're editing "widget.h" and find that you also need to update "widget.cpp".
It's good to keep your namespaces in separate folders. Nest namespaces in the same manner as they are nested within your project. For instance, if you have:
namespace Foo{ namespace Bar{ } }
then you'd want any objects in the Bar
namespace to be found in
{Foo's parent folder}\Foo\Bar\{how you're organizing code at this level}
We use an include folder for headers, a source for .cpp, a test folder for unit tests, and an object folder for compiled code bits. The reason we separate them is that it makes it easier to package the code in our scripts. You're always going to pass around the headers, you won't be passing around the source. (Here is another SO thread discussing separating header/source files. It's a preference thing.)
Here is a link to Google's style guidelines, if that helps.
- I don't separate headers from source : it's a pain to browse
I usually have the subdirectories match my namespaces
+ Project root + <project_name> // namespace project - sub_dir_1 // namespace project::sub_dir_1 - sub_dir_2 // namespace project::sub_dir_2
I only add "Project root" as an additional include path, so includes have the form :
#include "project/sub_dir_1/a.h" #include "project/sub_dir_2/b.h"
Because source and header are usually named according to the class they contain, the whole qualified named can be deduced from the include path :
project::sub_dir_1::a
is found inproject/sub_dir_1/a.h
a.c
trivially includesa.h
(relative path provided)- If I must include
a.h
fromb.h
, I use absolute path (starting from theproject/
root)
精彩评论