I have a main directory A
with two sub directories B
and C
.
Directory B
contains a header file structures.c
:
#ifndef __STRUCTURES_H #define __STRUCTURES_H typedef struct __stud_ent__ { char name[20]; int roll_num; }stud; #endif
Directory C
contains main.c
code:
#include<stdio.h> #include<stdlib.h> #include <structures.h> int main() { stud *value; value = malloc(sizeof(stud)); free (value); printf("working \n"); return 0; }
But I get an error:
main.c:3:24: error: structures.h: No such file or directory main.c: In function ‘main’: main.c:6: error: ‘stud’ undeclared (first use in this function) main.c:6: error: (Each undeclared identifier is reported only once main.c:6: error: for each function it appears in.) main.c:6: error: ‘value’ undeclared (first use in this function)
What is the correct way to include the structures.h
file into main.c
?
You can find this option under Project Properties->Configuration Properties->C/C++->General->Additional Include Directories. and having it find it even in lib\headers. You can give the absolute or relative path to the header file in the #include statement.
When header files are protected against multiple inclusion by the #ifndef trick, then header files can include other files to get the declarations and definitions they need, and no errors will arise because one file forgot to (or didn't know that it had to) include one header before another, and no multiple-definition ...
You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.
How to Include a Header File. To use the functions, data types, and macros defined in a header file, you must import them to your program. To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling the rest of the code.
When referencing to header files relative to your c file you should use #include "path/to/header.h"
The form #include <someheader.h>
is only used for internal headers or for explicitly added directories (in gcc with the -I
option).
write
#include "../b/structure.h"
in place of
#include <structures.h>
then go in directory in c & compile your main.c with
gcc main.c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With