What does the code include in C?

Please, could you tell me what this code does below?

...code...
#include file.h
...code...

I was used to include at the beginning of each file. I have never seen this before, and also could not find anything on the Internet.

+4
source share
3 answers

#includeis a preprocessor directive that takes the file specified as an argument and displays its contents in the current file. This is typically used to include commons function definitions from header files, but there is no need to use them that way.

+3
source

, , , , , include, .

+3

, #include , ( , ). , #include . X macro wikipage, C wikipage.

MELT ( MELT...).

predef-monimelt.h () ,
MOM_PREDEFINED_NAMED( , id , ) .

MOM_PREDEFINED_NAMED(GET,_9dsak0qcy0v_1c5z9th7x3i,1573018885)
MOM_PREDEFINED_NAMED(HEAD,_47fatww79x6_vh8ap22c0ch,3922245622) 
MOM_PREDEFINED_NAMED(web_handler,_7sav6zery1v_24sa6jwwu6c,2339220870)
#undef MOM_PREDEFINED_NAMED

My monimelt.h file (the real header file) defines external pointers and enumeration, so it matters:

// declare the predefined
#define MOM_PREDEFINED_NAMED(Name,Id,H) extern momitem_t* mom_named__##Name;
#include "predef-monimelt.h"

/// declare the hash of the predefined as an enum
#define MOM_PREDEFINED_NAMED(Name,Id,H) mom_hashname__##Name = H,
enum {
#include "predef-monimelt.h"
};

My main.c file contains basically a routine:

  // if this routine is compiled, we are sure that all predefined hashes
  // are unique
  const momitem_t *
  mom_predefined_item_of_hashcode (momhash_t h) {
    switch (h) {
  #define MOM_PREDEFINED_NAMED(Nam,Id,Hash) case Hash: return mom_named__##Nam;
  #include "predef-monimelt.h"
    default:
    return NULL;
     }
  }

but my items.c contains the file predef-monimelt.htwice (for creating predefined items during initialization and defining their variables):

 void mom_create_predefined_items (void) {
   int nbnamed = 0;
 #define MOM_PREDEFINED_NAMED(Nam,Id,H) do { \
   mom_named__##Nam = mom_make_item_of_identcstr(#Id); \
   mom_named__##Nam->i_space = momspa_predefined; \
   mom_register_item_named_cstr (mom_named__##Nam, #Nam); \
   nbnamed ++; \
 } while(0);
 #include "predef-monimelt.h"
 } // end of mom_create_predefined_items

 // declare the predefined
 #define MOM_PREDEFINED_NAMED(Nam,Id,H) momitem_t* mom_named__##Nam;
 #include "predef-monimelt.h"

FWIW, MELT Monitor is a licensed GPLv3 + software.

+1
source

All Articles