#include <> files in different files

if I have several header files: let's 1.h,2.h,3.h say that all three header files have #include<stdlib.h>one of the files included in them. when I have to use all 3 header files in file c main.c it will have 3 copies #include<stdlib.h>after the preprocessor. How the compiler handles such a conflict. Is this a mistake or does it create any overhead?

EDIT: if there are no header protectors, what will happen?

+3
source share
7 answers

Another point: you can reuse a function (or an external variable) bazillion times, and the compiler will accept it:

int printf(const char*, ...);
int printf(const char*, ...);

, .

, include .

, . , .

+3

C :

#ifndef FOO_H
#define FOO_H

/* Header contents here */

#endif

, , , FOO_H - undefined; FOO_H, .

: . , C :

#ifndef FOO_H
#include <foo.h>
#endif

++ ( ).

+7

:

#ifndef __STDLIB_H
  #include <stdlib.h>
  #define __STDLIB_H
#endif

, stdlib.h, .

+5

, , .

, , .

:

. , .

, .

, , , .

+5

, stdlib.

: #ifdef , .

Microsoft #pragma once, , , ( )

, . , , .

+2

, include . stdlib.h urely : http://en.wikipedia.org/wiki/Include_guard, . (!), : #include A, #undef A_GUARD, #include A .

... .h .h? , , ++, . : http://en.wikipedia.org/wiki/Forward_declaration

, . , /, .

include #pragma !

+2

, , , , , , ( ). assert.h, , NDEBUG . C:

; , - , , ​​ , <assert.h> NDEBUG.

, /. , , ( assert.h, ). ( , #pragma), " ".

, , .

, :

int a;

a. .

, , - include, :

#ifndef H_HEADER_NAME_
#define H_HEADER_NAME_
/* header contents */
#endif

#pragma s. ( , .)

, , include guard :

  • E, ,
  • PRI, X,
  • LC_, ,
  • SIG/SIG_, ,

.. .. ( H_NAME_.)

, , , (: , ).

#ifndef SZ
#define SZ 1024
#else
#if SZ == 1024
#undef SZ
#define SZ 128
#else
#error "You can include me no more than two times!"
#endif
#endif
+1
source

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


All Articles