C #include sth dependency confused - c

I have the following code in main.c
#include "config.h"
#include "util.h"
and in config.h
#include "util.h"
and there is a normal function in util.h
as I have include the util.h twice, I expect it should be wrong
but unfortunately, it goes damn well when I type: gcc main.c config.c util.c
why is this right?

it should not cause an error
if you have next files:
inc.h, inc1.h and inc.h includes inc1.h and inc1.h includes inc.h, then it cause of error
in the header files made following agreement:
#ifndef HEADER_NAME_H
#defiene HEADER_NAME_H
//body of header file
#endif /*HEADER_NAME_H*/
where HEADER_NAME_H is "header_name.h" filename

C headers usually have header guards that prevent them to be included multiple times. For example:
#include <stdio.h>
#include <stdio.h> // multiple inclusion of stdio.h, that's fine
A header guard is placed at the top of the header:
#ifndef MYHEADER_H
#define MYHEADER_H
// content of the header file
#endif MYHEADER_H

Related

How to use the pre-processor directive #include in a multi-file arduino c project?

I've wrote some code but when it's in a single file I find the code overwhelming and so I've tried to seperate the code into 3 files. The first being the main section (SACode.c), the second for making a char/string dictionary (CharCharStarArray.h) and the last for storing constants (constants.h). The SACode needs to be able to access CharCharStarArray.h and constants.h. and so I've used the following code in SAcode.c
/* File CharCharStarArray.h*/
#ifndef CharCharStarArray.h
#include "CharCharStarArray.h"
/* File constants.h*/
#ifndef constants.h
#include "constants.h"
//more code
#endif
#endif
The header file CharCharStarArray.h also needs to access the constants.h file and so in the CharCharStarArray.h file I've included this snippet
#ifndef constants.h
#include "constants.h"
//more code
#endif
When compiled I get "redefinition" or "previously defined here" errors from the constants.h file
I've commented out the include constants code in the SAcode file and it compiles but this seems like bad practice. If I didn't include the CharCharStarArray.h file then SACode.c wouldn't be able to access the constants file
Here's a recreation of the problem
headefiles
#ifndef header1.h
#include "header1.h"
#ifndef header2.h
#include "header2.h"
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
/*header2*/
#endif
/*header1*/
#endif
header1.h
#ifndef header2.h
#include "header2.h"
int header1var = 5;
#endif
header2.h
int header2var = 3;

How to include a header privately in C

How to include a header file in c privately so that files that include the file that includes the header wont include the header.
for example:
main.c
#include "main.h"
main.h
#include "test.h"
// I want main.c not to include test.h
I would like to know how to do that
use a #define:
in main.c
// the define MUST be before main.h inclusion
#define IN_MAIN_C
#include "main.h"
in main.h
#ifndef IN_MAIN_C
#include "test.h"
#endif

C circular dependency with headers

I am working on a project where I have to follow guidelines on the organization of the files and I can't get it to compile.
To simplify it I have a main.h where I HAVE to define bool and some symbols to go along with it:
#ifndef main_h
#define main_h
#include <stdio.h>
#include "test.h"
typedef unsigned char bool;
#define TRUE 1
#define FALSE 0
#endif /* main_h */
Then the main.c HAS to use a type "num_seconds_t" and a function "test()", both of which have to be in a seperate file from main.
so I have my test.h:
#ifndef test_h
#define test_h
#include <stdio.h>
#include "main.h"
typedef int32_t num_seconds_t;
bool test(num_seconds_t var);
#endif /* test_h */
and my test.c:
#include "test.h"
bool test(num_seconds_t var){
num_seconds_t test = var;
return TRUE;
}
I don't think main.c would have any affect on this issue.
The error states unknown type "bool" in my test.h file, and I kind of understand why as when it hits the include for "test.h" within the main.h it starts going through that file before it has defined bool in main.h, then since main.h has "#ifndef main_h" when it hits "#include "main.h"" in the test.h it skips this and continues reading so bool never gets defined until after the test.h is done being read.
I am not sure if my understanding is correct, but what is the proper way of solving this. By simply moving the "#include "test.h"" to after the definition of bool it will compile, but in my big project I have many files that intertwine and coordinating the order of including these files would be very tough if not impossible.
Thanks
I'll present 3 solutions:
1) Your best option is to not #include test.h in main.h, since you don't need anything defined in test.h in main.h (you do need stuff from test.h in main.c, so test.h should be #includeed in main.c). This is good practice in general, as described in the comments (you don't want to #include something you don't need).
2) Your second best option is to move things from test.h and main.h to another file. In this case, you'd want to move your typedefs and #defines to another file like so:
typedef unsigned char bool;
#define TRUE 1
#define FALSE 0
typedef int32_t num_seconds_t;
and then include this file in main.h and test.h (or just in test.h because `main.h doesn't need these typedefs).
3) Your last and worst option is to carefully choose the order in which you include things. This is your worst option, since it gets very unwieldy with a large project, but I will show it nonetheless.
Leaving all your other files the same and doing:
// main.c
#include test.h
as the only include in main.c will solve your problem. When you try to compile this, only test.h will be #includeed in main.c. This will then include main.h, which has the typedefs you need for test.h. main.h won't include test.h again, since it has already been included. This is okay, because main.h doesn't actually need test.h.
You could also move your #include in main.h below your typedefs and include main.h in main.c. As I mentioned, however, option 3 is not a good option.
You should really do some combination of options 1 and 2 (in general, not just for this project).

C preprocessor #error in header file included in multiple source files

I have two source files, main.c and datamgr.c - and two header files, config.h and datamgr.h
The testing system we're using expects these files, and only these files.
main.c:
#include "datamgr.h"
#include "config.h"
int main() {
custom_type a = 1;
a = foo();
return 0;
}
datamgr.c:
#include "datamgr.h"
#include "config.h"
custom_type foo() {
custom_type a = 1;
return a;
}
datamgr.h:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.h:
#ifndef CONFIG_H
#define CONFIG_H
#ifndef SET_MAX_TEMP
#error "Max temperature not set."
#endif
#ifndef SET_MIN_TEMP
#error "Max temperature not set."
#endif
typedef custom_type uint16_t
#endif
Now, the problem is that I can only define SET_MAX_TEMP and SET_MIN_TEMP in main.c, but both main.c and datamgr.c need both the header files. So if I leave them undefined in datamgr.c I get a compiler error. However, if I do define them in datamgr.c and later overwrite them in main.c, I get a different compiler error.
Please, any assistance as to how to get this horrible setup to work would be greatly appreciated.
You can pass these defines directly while compiling:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
In datamgr.c do:
#define SET_MAX_TEMP
#define SET_MIN_TEMP
#include "datamgr.h"
#include "config.h"
#undef SET_MAX_TEMP
#undef SET_MIN_TEMP
In a comment, you said:
Because main.c is the file that our testing system uses to implement the test scenarios.
In that case, make sure that the testing system defines those macros in the command line of the compiler for every file being compiled.

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

Resources