best articles about organizing code files in C [closed]

拜拜、爱过 提交于 2019-12-02 19:19:58

A good book that covers a lot of this (for both C and C++) is Large Scale C++ Software Design, by John Lakos:

Also, a good rule of thumb to remember is "Never do anything that allocates memory in a header file"

Regarding the files layout there are not too many alternatives.

The partitioning is typically one of the following (package here is a single library or binary):

  1. .../project/.../package/module.{c,h}
  2. .../project/.../{src,include}/package/module.{c,h} // non-interface headers go to src
  3. .../project/.../package/{src,include}/module.{c,h} // non-interface headers go to src

The partitioning (1) is convenient in that all files belonging to particular package are stored in a single directory, so package can be easily moved around, but with this approach detaching API headers from private ones and detecting API changes is not trivial. (2) and (3) are very similar, they make API release and API changes detection trivial, while (2) is slightly easier for the case when you always release the whole project and (3) is slightly better when you release individial packages (e.g. for patching purposes)

In any C/C++ project there is typically the following common packages:

  1. Common macros, and data types
  2. Logging package
  3. Application bootstrap package (in case there are more than 1 binaries in the project).

Specific to unix (and not to c, natch), but none the less:

Recursive Make Considered Harmful

With the build structure described, you can afford to use a lot of files. So each logical unit gets a header and a source file.

Alex Fort

I think the best educational reading you're going to get on this subject, is reading something like the Linux Kernel source. It's got a good source layout, and is basically the standard large C project. Here is a guide for how source files should be put together for BSD source, as well.

Seriously, just start reading Kernel source and get a feel for how everything is put together. It's a very well planned project, obviously.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!