Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best articles about organizing code files in C [closed]

Can you recommend me what should I read/learn in order to make a well organized code in C?

One of the things I want to learn is the principles of splitting project in .h and .c files, what goes where and why, variable naming, when to use global variables ...

I am interested in books and articles that deal with this specific problem.

like image 949
kliketa Avatar asked Jan 28 '09 19:01

kliketa


2 Answers

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"

like image 82
George Sealy Avatar answered Sep 21 '22 09:09

George Sealy


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).
like image 25
bobah Avatar answered Sep 17 '22 09:09

bobah