When do I split a project into multiple C files? (best practices for large projects)

I am working on a large project in C right now, I am doing a certain part of it (the other is done by others), I want to know when my project should be split into several c files, and what are the best practices for writing large projects, with a team (or alone) ) My previous experience was in headings (with a heading and .c where all the functions were written).

+4
source share
2 answers

People will tell you different things. Generally:

  • You should split the file into smaller files if:
    • It has many dependencies.
    • Too much time to compile
    • He knows or does too much.
  • You must organize your project:
    • What parts work with which data types
    • What parts work with which other parts of the project
  • Things to avoid:
    • Headers that only work if they are included in certain orders
    • Headings that draw big drops of a project (too many dependencies)
    • Preprocessor macro violation
    • "util.c" or similar. From personal experience, they tend to get out of hand.

In C ++ (not C), I usually have a .h and .cpp file for each main class that I write, and sometimes I can raise some supporting classes to files with other classes, to which they are very closely related , and without which they are more or less meaningless (anecdotally, I have an average project containing classes such as Scheduler and SchedulerEvent in the same file in which the scheduler contains the SchedulerEvents collection. The project has about 180 files and is still simple to maintenance).

+2
source

Typically, put a type definition (or definition of a close type) and a function to process that type (s) in one module.

The open type and functions are included in the .h file, the private type and functions, and all implementations are included in the .c file.

.h files without .c files are rare.

0
source

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


All Articles