组织项目目录的一种流行方式或多或少是这样的:
我的库
+--mylib_class_a.h
mylib_class_a.cpp
mylib_library_private_helpers.h
mylib_library_private_helpers.cpp
我的应用
+--other_class.h
other_class.cpp
应用程序.cpp
app.cpp:
#include "other_class.h"
#include <mylib_class_a.h> // using library MyLib
同一个库的所有.h和.cpp文件都在同一个目录中。为避免名称冲突,文件名通常以公司名称和/或库名称作为前缀。MyLib 将位于 MyApp 的头文件搜索路径等中。我不喜欢为文件名添加前缀,但我喜欢查看#include并确切知道该头文件所属位置的想法。我不讨厌这种组织文件的方法,但我认为应该有更好的方法。
由于我正在开始一个新项目,因此我想征求一些目录组织的想法。目前我喜欢这个目录结构:
项目
+--包括
+--项目
+--mylib
+--class_a.h
+--应用程序
+--other_class.h
+--src
+--mylib
+--class_a.cpp
library_private_helpers.h
library_private_helpers.cpp
+--应用程序
+--other_class.cpp
应用程序.cpp
实用程序.h
app.cpp:
#include "util.h" // private util.h file
#include <ProjA/app/other_class.h> // public header file
#include <ProjA/mylib/class_a.h> // using class_a.h of mylib
#include <other3rdptylib/class_a.h> // class_a.h of other3rdptylib, no name collision
#include <class_a.h> // not ProjA/mylib/class_a.h
#include <ProjA/mylib/library_private_helpers.h> // error can't find .h
.cpp文件和私有(仅对直接库可见).h文件存储在 src 目录下(src 有时称为 lib)。公共头文件被组织成一个 project/lib 目录结构,并通过<ProjectName/LibraryName/headerName.h>. 文件名没有任何前缀。如果我需要打包 MyLib 以供其他团队使用,我可以简单地更改我的 makefile 以复制适当的二进制文件和整个 include/ProjA 目录。
一旦文件被检入源代码控制并且人们开始处理它们,就很难更改目录结构。最好一开始就做好。
任何有组织这样的源代码经验的人?你有什么不喜欢的吗?如果你有更好的方法,我很想听听。