如何在C++中逻辑地组织源文件

b1n*_*phy 9 c++ c++builder organization

我的源文件窗口正在快速增长(就我项目中的文件数量而言),并且在任何给定时间快速找到我需要访问的特定源文件会变得有点麻烦.我正在使用Embarcadero的C++ Builder,但我也在其他C++ IDE中遇到过这个问题.

在Java中,我经常使用包来创建源代码的逻辑分区,尤其是在单个项目中处理大量源文件时.虽然这,当然,是不是Java包的唯一目的,他们在这方面是非常方便的.

有没有人对如何在C++中实现类似的功能有任何想法?我应该将我的源分成物理文件夹吗?C++ Builder是否提供了某些我没有看到的虚拟文件夹/分组功能?任何想法都表示赞赏,谢谢.

Dev*_*lar 10

我通常建议不要(仅)使用IDE或语言语法来组织源代码.首先,你将自己绑定到环境:在IDE中组织良好,文件无组织,然后在你可能想要使用不同环境的那一天...

因此,我通常使用所有三种方式同时组织我的源:我将我的源分为功能模块,即相关类.每个模块都有自己的命名空间,物理文件夹和IDE文件夹.(在我的例子中,使用CMakesource_group()在需要时生成IDE项目文件 - 个人更喜欢命令行,Vim和"make".)

因此,无论我是在IDE中,从命令行还是从编译器日志中查看项目,foo/some_class.hpp都是foo/some_class.cpp是foo :: some_class,最大限度地减少了混乱.

实际上,我目前首选的设置进一步将每个模块目录细分为<project>/<module>/<class>.hpp或者<project>/<module>/src/<class>.hpp取决于该类是否在其自己的模块之外使用<project>/<module>/src/<class>.cpp,和<project>/<module>/test/<class>_tu.cpp.<project>::<module>::<class>当然是命名空间.

project
|-- foo
|   |-- some_class.hpp
|   |-- src
|   |   |-- internal_class.hpp
|   |   |-- internal_class.cpp
|   |   `-- some_class.cpp
|   `-- test
|       |-- internal_class_tu.cpp
|       `-- some_class_tu.cpp
|-- bar
|   |-- ...
Run Code Online (Sandbox Code Playgroud)

这里的想法是每个module(foo)的"外部"接口由该子文件夹中的头文件记录,实现细节和测试"隐藏"在相应的子文件夹中.

但最终,这在很大程度上取决于您的品味,您的共同开发人员的品味以及项目的范围.


Mat*_*son 10

这是我的风格:

PROJECT_NAME
|-- build // This is DVCS ignored but has all the built intermediates and final binaries
|   |-- release // These are the different build profiles
|   |-- debug
|   |-- profile
|   `-- coverage
|-- bin // For binary source code
|   `-- hello_world
|       |-- doc
|       |-- inc
|       |-- src
|       |-- tests
|       `-- build_script  // Builds binary into the build folder
|-- include // Public headers for the library
|   `-- these
|       `-- folders
|           `-- represent
|               `-- namespaces
|                   `-- my_awesome_class.hpp
|-- lib // library source code
|   |-- these
|   |   `-- folders
|   |       `-- represent
|   |           `-- namespaces
|   |               |-- inc // Private headers
|   |               |   `-- my_private_class.hpp // internal class
|   |               |-- src // Source code for this namespace
|   |               |   |-- posix
|   |               |   |   `-- my_awesome_class.cpp // posix specific source code
|   |               |   |-- nt
|   |               |   |   `-- my_awesome_class.cpp // nt specific source code
|   |               |   |-- my_private_class.cpp // non-visibile class
|   |               |   `-- my_awesome_class.cpp // cross platform source code
|   |               |-- tests // Unit tests
|   |               |   `-- my_awesome_class.cpp // builds a test executable for the library
|   |               `-- doc // Documentation for this namespace
|   |                   `-- namespace.dox
|   `-- build_script  // Builds binary into the build folder
|-- doc // Documentation files
|   |-- main_page.dox
|   `-- namespace.dox
`-- build_script  // Builds the source code into the build folder
Run Code Online (Sandbox Code Playgroud)

这表示these::folders::represent::namespaces::MyAwesomeClass它具有类posixNT特定的源代码(以及通用源代码),再加上有一个私人these::folders::represent::namespaces::MyPrivateClass是在图书馆内部使用,头都没有公众和知名度类的符号是隐藏的.

这已经很好地扩展并且提供了容易的文件定位.