【问题标题】:How to logically organize source files in C++ [closed]如何在 C++ 中逻辑地组织源文件 [关闭]
【发布时间】:2013-04-14 21:37:50
【问题描述】:

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

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

有人对我如何在 C++ 中实现类似功能有任何想法吗?我应该将源文件分成物理文件夹吗? C++Builder 是否提供了某种我没有看到的虚拟文件夹/分组功能?感谢您的任何想法并感谢您。

【问题讨论】:

  • 我通常通过类浏览器导航,而不是依赖源文件组织。
  • 组织成文件夹。如果它的大模块,我建议把它做成一个项目
  • 天哪,我是不是问错人了。我对组织非常陌生,我的 IDE 很少打开三个以上的源文件,而且我的项目总是设置一个 src/ 文件夹,如果事情开始变得棘手(他们很少这样做,因为我是不是大垃圾箱倡导者)。还有一个项目 include/ 用于交叉功能的文件夹。
  • Visual Assist X 有助于在 Visual Studio 中高效定位源文件。与 Xcode 甚至 KDevelop 相比,VS 在该部门确实缺乏。
  • 我与 Whole Tomato 没有任何从属关系。我最近开始在 Windows 上工作,并且确实受到了 Visual Studio 的缺陷的困扰,而我上面提到的工具提供了帮助。是的,我同意它是真正应该在 VS 中的标准功能。但事实并非如此。

标签: c++ c++builder organization


【解决方案1】:

我一般建议不要(仅)使用 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
|   |-- ...

这里的想法是每个模块 (foo) 的“外部”接口由该子文件夹中的标题记录,实现细节和测试“隐藏”在各个子文件夹中。

但最终,这在很大程度上取决于您的品味、您的合作开发者的品味以及您项目的范围。

【讨论】:

  • 这似乎是全方位的最佳解决方案。特别是如果我的项目将来迁移到不同的 IDE,因为它将涵盖我的所有方面。感谢您的帮助。
  • @b1nary.atr0phy:也许您可能对JAWS(我在此期间设置的东西)感兴趣...
【解决方案2】:

这就是我的滚动方式:

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

这表示these::folders::represent::namespaces::MyAwesomeClass 类,它具有posixNT 特定源代码(以及通用源代码),还有一个在库内部使用的私有these::folders::represent::namespaces::MyPrivateClass,标头不是public 和 visibility 的类符号被隐藏。

这已经很好地扩展并提供了轻松的文件定位。

【讨论】:

  • out of source build 也很整洁。我建议删除build 目录并从../build_whatever_mode 构建。
【解决方案3】:

我制作项目是为了让我的所有文件都易于访问。这是最简单的组织方式,还有清晰的类/文件名。

【讨论】:

  • 我实际上指出(实际上是在我的第一句话中)我正在处理的一个项目。我的问题是组织该项目中的源文件。在处理相对大量的类时,仅仅给它们明确的名称是不够的。
猜你喜欢
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2013-08-06
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
相关资源
最近更新 更多