including header files in one another - c

if I have two header files
a.h and b.h
can I include "a.h" in b.h
and also include "b.h" in "a.h" ?

You can, but it's not a very good idea. If you really must, you can prevent recursion with the use of include guards (which are a good idea regardless).
In a.h:
#ifndef A_H
#define A_H
#include "b.h"
#endif
and b.h
#ifndef B_H
#define B_H
#include "a.h"
#endif

No that won't work. The preprocessor just replaces your #include"xyz.h" with the actual file, so this would end in an endless recursion.

Related

Header is transitive?

If I have 3 files:
header1.h
header2.h
header3.h
And I did #include "header1.h" in header2.h, then if I do #include "header2.h" in header3.h does header3.h have header1.h's declarations?
Yes, provided that there are no conditional compiling statements that may prevent such inclusions from happening.
For example, in this situation, header1.h won't be included from header3:
header2:
...
#ifndef THING
#include "header1.h"
#endif
...
header3:
...
#define THING
#include "header2.h"
...

How can I avoid redefinition of types while including multiple header files?

I'm working on recompiling a C project and I'm not sure how do I fix this problem in a right way.
Here is a situation -
a.h
#ifndef A_H
#define A_H
typedef int INT;
// other variables and function definition
#endif
b.h
#ifndef B_H
#define B_H
typedef int INT;
// other variables and function definition
#endif
main.c
#include "a.h"
#include "b.h"
int main()
{
INT i = 10;
return 0;
}
The error I get in Linux with gcc:
In file included from ./main.c,
./b.h:<linenumber>: error: redefinition of typedef ‘INT’
a.h.h:<linenumber>: note: previous declaration of ‘INT’ was here
I have to include both headers due to other variables and functions. I haven't written this code, but this seems to compile in my Solaris environment which is strange. What can I do to fix this ?
Probably the native compiler on Solaris accepts that you can redefine a typedef (probably provided that the new typedef is identical to the previous one which is the case here).
I'd introduce another header file mytypes.h like this:
mytypes.h
#ifndef MYTYPES_H
#define MYTYPES_H
typedef int INT;
#endif
Include mtypes.h whereever INT is used, possibly even in main.c:
a.h
#ifndef A_H
#define A_H
#include "mytypes.h" // can be removed if INT is not used in a.h
// other variables and function definition
#endif
b.h
#ifndef B_H
#define B_H
#include "mytypes.h" // can be removed if INT is not used in b.h
// other variables and function definition
#endif
main.c
#include "a.h"
#include "b.h"
#include "mytypes.h" // not really necessary because it's already included
// via a.h and b.h, but still good practice
int main()
{
INT i = 10;
return 0;
}
If you are allowed to change the library code or compiler options then Michael Walz's answer is the way to go
In the unfortunate case that it's not changeable then it can be worked around by renaming before including the header then undefine it
#define INT INT_A
#include "a.h"
#undef INT
#define INT INT_B
#include "b.h"
#undef INT
Now just use INT_A for all the interfaces in a.h instead of INT. Same to INT_B in b.h

How to have the compiler put a header file in a source file

I know you can put a header file on top of a file by using the -include compiler flag in gcc, but is it possible to include the header file at the end of other header file declarations of a file. So for example, I have the following declarations in a C source file.
#include "a.h"
#include "b.h"
I would like it, to become
#include "a.h"
#include "b.h"
#include "inserted.h"
rather than
#include "inserted.h"
#include "a.h"
#include "b.h"
Use
-include a.h -include b.h -include inserted.h
Add header protection to all *.h files (which should be there anyway ... ;-).
You can't do exactly what you are asking. There is no way to tell the compiler to insert a header file at a random point in a file. But maybe you can get something close.
First make all the declarations in inserted.h as a macro:
#define DECLARE_INSERTED_H \
int gFoo = 0; \
void functionBar(); \
Then in your c file:
#include a.h
#include b.h
#ifndef DECLARE_INSERTED_H
#define DECLARE_INSERTED_H
#endif // !DECLARE_INSERTED_H
DECLARE_INSERTED_H
Then compile with -i inserted.h

How to include a specific header file without having a conflict

My Files are
main.c
#include"A.h"
#include"B.h"
A.c
#include"A.h"
B.c
#include"B.h"
I have a file with a couple of structures that I have defined that I am supposed to use in all the files i.e A.c , B.c, main.c and even the header files for A and B.
Hence I have
A.h and B.h both have
#include"struct.h"
Now, I see that in my main.c
I will have multiple declaration for both the structures, how do I get rid of this problem. What shall I change in my structure?
Thanks
Use include guards.
aheader.h:
#ifndef AHEADER_H
#define AHEADER_H
// ... rest of header here
#endif
bheader.h:
#ifndef BHEADER_H
#define BHEADER_H
// ... rest of header here
#endif
You can use a guard as such,
#ifndef MY_STRUCT
#define MY_STRUCT
#include "struct.h"
#endif
If you want to selectively take care of which parts should not be duplicated
Wrap the header files in include guards., like this:
#ifndef MYHEADER_H
#define MYHEADER_H
// your definitions
#endif
Each header file should have its own guard with an unique name. The above preprocessor directives translated to english say something like: "If MYHEADER_H is not defined, then define it and paste the contents until #endif directive." This guarantees that a single header is included only once inside a single translation unit.
Simply use so called header guard to be sure of including "struct.h" only once:
// struct.h
#ifndef STRUCT_H
#define STRUCT_H
struct ...{
}
#endif

Is it possible to #Include in a "diamond inheritance" structure?

I'm trying to make some project in C.
I would like to know if it is possible to make #include from the same file twice, in a way that recalls diamond heritage.
i.e.
in a.c there is #include "a.h"
in b.c there is #include "b.h"
in b.h there is #include "a.h"
Is it possible to #include "b.h" in a.c?
I get an error:
some_variable already defined in a.obj
Simple: don't define variables in headers, just declare them:
Header:
// a.h
#ifndef A_H // always use #include guards
#define A_H
extern int my_variable; // declare my_variable
...
#endif
Source file a.c:
// a.c
#include "a.h"
int my_variable; // define my_variable
...
Source file b.c:
// a.c
#include "a.h"
#include "b.h"
...
As others have mentioned, #include guards are useful, and a good habit to get into, but they are probably not the solution for this specific problem.
You have to declare extern the variables in a.h, then modify your header a.h in the following way:
#ifndef a_h
#define a_h
//your a.h
#endif

Resources