C language, correct use of included guards

I am trying to create a header file (which will include the functions I wrote for AVL Trees), but I have a little problem and a misunderstanding of the syntax of the guards included.

Right now my code looks like this

#ifndef STDIO_H
#define STDIO_H
#endif
#ifndef STDLIB_H
#define STDLIB_H
#endif
#ifndef CONIO_H
#define CONIO_H
#endif

      

the problem I think it only includes <stdio.h>

. When I try to use malloc it says malloc is undefined even though I have included stdlib.

According to http://www.cprogramming.com/reference/preprocessor/ifndef.html , if I understood correctly, ifndef checks if a token is defined, and if it isnt, it defines everything I write after ifndef and before # endif. Therefore my code should work.

Is stdio defined? not. so define it. ENDIF. is stdlib defined? not. so define it. ENDIF. defined by conio? not. so define it. ENDIF. I don't see the problem.

What is the correct syntax if I want to add these 3 headers?

+3


source to share


5 answers


Enabling guards is used to prevent double detection if the included file is enabled more than once.

The standard files include the required protective devices, so you don't need to enable protective devices for them.



Your code should be:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

      

+4


source


The next ad will not have a headline stdio.h

.

#ifndef STDIO_H
#define STDIO_H
#endif

      

If you say so, it does not mean that it will contain your header file stdio.h

. Its the most suitable method for "Custom Headers".

You need to include all declaration and function definitions as shown below in your own header file (avltree.h) -



#ifndef AVLTREE_H
#define AVLTREE_H

/* YOUR HEADER FILE STUFF */

#endif

      

Then include this header file in your main program.

stdio.h

, stdlib.h

And conio.h

are available header files, you can directly include all files in the main program file -

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

      

+2


source


You put the included guards in your own headers.

How in:

//GameEntity.hpp
#ifndef __H_GAME_ENTITY
#define __H_GAME_ENTITY

class GameEntity{
  //whatever
};

#endif

      

Then it will only be included once per compilation block.

Now something like this won't work:

#include <GameEntity.hpp>
#include <GameEntity.hpp>

int main(){ return 0; };

      

+1


source


The correct syntax for adding these three headers is:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

      

#ifndef

is a preprocessor directive followed by a token, let it be X

. It will check if it was X

defined via a #define

preprocessor directive , and if not, subsequent lines will be processed by your compiler until appropriate #endif

. For example:

// #define X
// #define X 123456

#ifndef X
/* some code */
#endif

      

The thing above can be read as if X was not defined. The part /* some code */

will be considered only if it is X

not defined, in which case it is X

not, so it will be. If I refused to comment on one of the #define ...

above, the part /* some code */

would be ignored by the compiler.

Turn on the guard is what this thing I explained above is using #ifndef

. You don't need to worry about this until you get to the point of creating the header files yourself.

Header files usually (usually) have #include guard

s
inside them. No matter what they do, they first check if a specific token is defined or not. If not, then they will define this token themselves and do whatever. If it has already been defined, then they do nothing. This is to prevent unwanted multiple definitions of what's inside. For example, if you want to check <stdio.h>

MSVC 2013, you will see it starts and ends like this:

#ifndef _INC_STDIO
#define _INC_STDIO

// hundreds of lines in between

#endif

      

Thanks to this, if you were to write something like:

#include <stdio.h>
#include <stdio.h>

// ...

      

In your code, the second one #include

will do almost nothing, because the first one will already execute the line #define _INC_STDIO

that defines _INC_STDIO

and prevents almost everything <stdio.h>

from being executed in and then included.

This is not to prevent me from apologizing for "silly" programmer mistakes, although it is useful when the header file contains another header file. For example, as <stdio.h>

well as <stdlib.h>

in MSVC 2013 attempt to include <crtdefs.h>

as a first step. Now, if it <crtdefs.h>

were included twice, the set of type definitions inside would be defined multiple times, and they shouldn't. I can of course write the following above my code:

#include <stdio.h>
#include <stdlib.h>

// ...

      

And the #include

guards would save the day for me by preventing the content from being executed multiple times <crtdefs.h>

.

0


source


As in the previous comments and everyone pointed out that you didn't include anything in your main header file.

Even I am also preparing a single header file and I prefer to do it like this:

Suppose you have a main header file "includes.h" in which you include the entire header file. Then

#ifndef INCLUDES_H
#define INCLUDES_H

#ifndef STDIO_H
     #include <stdio.h>
#endif

#ifndef STDLIB_H
     #include <stdlib.h>
#endif

#ifndef CONIO_H
     #include <conio.h>
#endif

//If you have any of your own header files, then include them the same way
#ifndef USER_AVL_HEADER_FILE
     #include <user_avl_header_file.h>
#endif    

#endif   // INCLUDES_H

      

And then, as you have the name of your header file "user_avl_header_file.h" then in that header file set the header protection again:

#include "includes.h"

#ifndef USER_AVL_HEADER_FILE
#define USER_AVL_HEADER_FILE

/*your class and your code*/

#endif    //USER_AVL_HEADER_FILE

      

and in your source file just don't include the main header file "includes.h" without any problem.

-1


source







All Articles