Tips / resources for structuring C code?

Does anyone have any tips / resources on how to best structure your C code projects? (Various folders, etc.) And how do you know when it is good to split the code into separate files? What is a Makefile example?

My project is not so big, but I want to start structuring my code at an early stage.

+3
source share
4 answers

Structuring code requires some experience, but mostly common sense.

For code separation, you usually read: conceptually consistent functions / data types should go in one file. As a good example, you can take the standard library. It is better to keep data definitions and descriptions in separate headers. This allows you to use data structures as part of the compilation module, even if you have not defined all the functions.

Files that provide similar functionality should be in the same directory. It is good to avoid a deep directory structure (preferably 1 level of depth), as this makes it difficult to create a project unnecessarily.

I think Make files are fine for small projects, but are cumbersome for larger ones. For really serious work (if you want to distribute your code, create an installer, etc.), you can look at cmake, scons, etc.

GNU: http://www.gnu.org/prep/standards/standards.html

gnu Makefile. Makefile. sourceforge.net.

+2

C, , , . :

C:

+1

, - . , .

0

, :

struct , . init deinit :

  • - struct foo*.
  • struct foo*.
  • , int* (), enum foo_error* ( , , ) GError** ( GLib-).

foo_init() foo_deinit() return NULL, NULL. .

? , . , , :

struct foo* a_foo = foo_init(malloc(sizeof(*a_foo)));
if (a_foo == NULL) {
  /* Ruh-oh, allocation failure... */
}
free(foo_deinit(a_foo));

Everything works well, even if a_foo == NULLon call foo_deinit.

0
source

Source: https://habr.com/ru/post/1775164/


All Articles