How to include a header privately in C - 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

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;

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.

C #include sth dependency confused

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

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

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