C-structure of the project - heading module for one big heading

I worked with several C projects during my programming career, and header file structures usually fall into one of these two patterns:

  • One header file containing all function prototypes
  • One .hfile for each file .ccontaining prototypes for functions defined only in this module.

The advantages of option 2 are obvious to me - it makes it cheaper to split a module between several projects and makes it easier to view dependencies between modules.

But what are the benefits of option 1? It must have some advantages, otherwise it will not be so popular.


This question applies to both C ++ and C, but I have never seen # 1 in a C ++ project.

Placement #defines, structetc. also changing, but for this question I would like to focus on function prototypes.

+5
source share
9 answers

Option 1 allows you to have all the definitions in one place, so you need to include / search only one file, and not include / search many files. This advantage is more obvious if your system is delivered as a library to a third party - they do not really care about your library structure, they just want to use it.

+3
source

, β„– 1 ... . , , , / , - "", .

, , , " ", - , .

+8
  • 1 . , , .

# 2 :

  • #ifndef _HEADER_Namefile
    #define _HEADER_Namefile_
    

    #endif

( ), .

  • ... -, , / , , . , , C, extern:

    extern unsigned long G_BEER_COUNTER;
    

, G_BEER_COUNTER unsigned long (, ), - /. ( / .) ( extern) .c. .

  • . include .c(). / .
+4

.h .c . .h( , .c ), , .h , .c . , , , , .

+4

: , , , , , . , ( , ), .

+4

: .c .h, .h, .h. .h , .

.h, .h .

+3

/ , , . , .

+1

++ , .

, -

0

, /. 10 , , , -, .

, STL, , , . GCC 8 - , , , , bvector. 8 , ?

, . , .

0

All Articles