Home > database >  Using #include in header files
Using #include in header files

Time:01-29

Assuming we have 2 files DIO.c and DIO.h. I used to #include all necessary header files in DIO.h and only #include DIO.h in DIO.c. while working on drivers for ATmega32, I found out that some functions are declared implicitly while including other modules. This made me wonder is that right to include all files in header file instead of source file? and how what i'm doing currently affects dependency?

After visiting some repos on github i found out that they never #include anything in header files, and only include everything in C file, which made me more confused about what about typedefs? how they use them without including the header file containing them?

Here is an example of DIO.h file:

#include "Dio_Private.h"
#include "Dio_Config.h"
#include "../../Utilities/Macros.h"

and here is an example of DIO.c file:

#include "DIO.h"

If the answer was that I should include all header files in C file, what about enums?

for example, if I want to use enums defined in DIO.h file in any other module, should I now seperate these enums in another header file and only #include it?

Thanks in advance :)

CodePudding user response:

It's perfectly fine to #include multiple headers in one and use that (provided you use proper include guards).

Having said that, if you don't like typing multiple #includes over and over and you are thinking of creating a big one just for convenience, keep in mind that this will affect compilation time (since the contents of all the headers have to be parsed for every .c file that includes them).

CodePudding user response:

@ShaneGervais is right that you should use include guards, and right that #include is somewhat like pasting the contents of the header file into your source file, but incorrect about the rest of it.

Example Dio_Private.h

#ifndef DIO_PRIVATE_H_
#define DIO_PRIVATE_H_

int dinput(char **);

int doutput(char *);

#endif

This will ensure no errors on multiple #include

Do not use #pragma once. Do not use #define __DIO_PRIVATE_H__. These are not standard C and the latter one results in undefined behavior.

Do not define your functions in a header file, especially as a beginner. For very small, very succinct functions that do not use global variables and which will not bloat your code too much if used several times over, it may be appropriate, when you more fully understand how to use them, to define them in a header file. Example:

#ifndef MYSTRDUP_H_
#define MYSTRDUP_H_

#include <stdlib.h>
#include <string.h>

static inline char *mystrdup(const char *s) {
    size_t n = strlen(s)   1;
    char *r = malloc(n);
    if (r) memcpy(r, s, n);
    return r;
}
#endif

CodePudding user response:

The biggest thing when using #include in anything is that its literally like copy and pasting whatever that's in the file and "including it" or "pasting it" in the file.

If you do this in the DIO.h file:

#include "Dio_Private.h"
#include "Dio_Config.h"
#include "../../Utilities/Macros.h"

then do in the DIO.c file:

#include "DIO.h"

its like you copy pasted everything that's in your DIO.h file and pasted it in the DIO.c file therefore include whatever the .h file includes.

If you did this in the DIO.c file:

#include "DIO.h"
#include "Dio_Private.h"
#include "Dio_Config.h"
#include "../../Utilities/Macros.h"

You'll get an error because you've basically redefined whatever functions that are in those files twice.

You could use logic operators to check if they've already been included in your file (in DIO.h):

#ifndef
#define _DIO_H_
whatever you want in this file
#endif

short hand way

#pragma once
whatever you want

So it depends how you want to structure your files. If you want to use the DIO.h file more then one place where every time you use the DIO.h file you want to use the others then you should have them in your header but with the logic operation to check if they exist. If you don't want to use everything that's in the other files and want to treat it seperately from then don't include them in DIO.h

Hope this makes sense.

  • Related