Where to put the included statements, title or source?

Should I put them in a header file or source file? If the header file contains include statements, then if I include this header file in my source code, will there be all the files in it that were in my header in my source file? Or should I just include them only in the source file?

+78
c include header
Oct. 15 2018-10-15
source share
9 answers

Only put includes in the title if the title itself needs them.

Examples:

  • The function returns type size_t . Then #include <stddef.h> in the header file.
  • Your function uses strlen . Then #include <string.h> in the source file.
+85
Oct. 15 2018-10-15
source share

Over the years there have been quite a few disagreements. At one time it was traditional that a heading only declares what was in any module to which it was connected, so many headings had special requirements for you to #include specific set of headings (in a certain order). Some extremely traditional C programmers still follow this pattern (religiously, at least in some cases).

More recently, there has been a movement towards making most of the headings autonomous. If this header requires something else, the header itself processes this, ensuring that everything it needs is included (in the correct order if there are problems with ordering). Personally, I prefer this - especially when the order of the headers can be important, it solves the problem once, instead of requiring everyone who uses it to solve the problem again.

Please note that most headlines should only contain ads. This means that adding an unnecessary header should not (usually) have any effect on your final executable. Worst of all, it slows down compilation a bit.

+20
Oct. 15 2018-10-15
source share

Your #include should consist of header files, and each file (source or header) should #include header files that it needs. Header files should #include use minimal header files as well as source files, although this is not so important for the source files.

The source file will have #include s headers, and #include headers, etc. to the maximum depth of nesting. That's why you donโ€™t want the extra #include in the header files: they can cause the source file to contain many header files that it might not need, slowing down compilation.

This means that it is possible that the header files can be included twice, and this can be a problem. The traditional method is to include โ€œinclude guardsโ€ in the header files, for example, for the file foo.h:

 #ifndef INCLUDE_FOO_H #define INCLUDE_FOO_H /* everything in header goes here */ #endif 
+9
Oct. 15 2018-10-15
source share

If the header file A #includes header files B and C, then each source file that #includes A will also receive B and C #included . The preprocessor literally simply replaces the text: wherever it finds text that says #include <foo.h> , it replaces it with the text of the foo.h file.

There are different opinions on whether to include #includes in headers or source files. Personally, I prefer to put all #includes in the default source file, but any header files that cannot be compiled without other preliminary headers should #include these headers themselves.

And each header file must contain an included protector to prevent it from being included multiple times.

+3
Oct. 15 2018-10-15
source share

Make all of your files so that they can be built using just what they include. If you do not need to include in your title, delete it. In a large project, if you do not support this discipline, you leave yourself open to crack the entire assembly when someone removes the inclusion from the file of the header that is used by the consumer of this file, and not even the header.

+2
Oct 15 2018-10-15
source share

In some environments, compilation will be the fastest if it includes only header files. In other environments, compilation will be optimized if all source files can use the same main collection of headers (some files may have additional headers outside of a common subset). Ideally, headers should be built so that multiple #include operations have no effect. It may be useful to surround #include statements with checking the include-guard file included, although this creates a dependency on the format of this defender. In addition, depending on the caching behavior of the system file, an unnecessary #include, whose goal ends entirely in # ifdef'ed away, may not take a lot of time.

Another thing to keep in mind is that if a function takes a pointer to a structure, you can write the prototype as

 void foo (struct BAR_s * bar);

no definition for BAR_s, which should be in scope. A very convenient approach to prevent unnecessary inclusions.

PS - in many of my projects there will be a file that expects each module to be #include containing things like typedefs for integer sizes and several general structures and unions [for example,

 typedef union {
   unsigned long l;
   unsigned short lw [2];
   unsigned char lb [4];
 } U_QUAD;

(Yes, I know that I would have problems if I switched to a large-end architecture, but since my compiler does not allow anonymous structures in unions, using named identifiers for bytes in a union will require them to access them as theUnion. b.b1, etc., which seems pretty annoying.

+2
Oct. 15 2018-10-15
source share

The approach that I have developed over twenty years is as follows:

Consider the library.

There are several C files, one internal H file and one external H file. C files include internal file H. Internal file H contains external file H.

You see, of the POV compilers, when it compiles a C file, there is a hierarchy;

external โ†’ internal โ†’ code C

This is the correct order, since the external is all that a third party should use in the library. Compiling C code requires something that is internal.

+1
Oct 15 '10 at 15:01
source share

Include statements will be included in the source file if you put it in the header. However, in some cases, it would be better to put them in the source file.

Remember that if you include this heading in any other sources, they will also receive inclusions from the heading, and this is not always desirable. You should include only the material in which it is used.

0
Oct 15 2018-10-15
source share

You should include only the files in your header that need to be declared constants and function declarations. Technically, they will also be included in your source file, but for clarity, you should only include the files you really need in each file. You should also protect them in your header from multiple inclusion:

 #ifndef NAME_OF_HEADER_H #define NAME_OF_HEADER_H ...definition of header file... #endif 

This prevents the header from being included several times, resulting in a compiler error.

0
Oct 15 2018-10-15
source share



All Articles