Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a header file from another directory

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?

like image 613
Manny Avatar asked Sep 28 '11 09:09

Manny


People also ask

How do I insert a header from another folder?

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.

Can I include one header file in another?

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 ...

Which one is another way to include header files?

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 do I import a header file?

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.


2 Answers

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).

like image 58
Constantinius Avatar answered Sep 22 '22 01:09

Constantinius


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 
like image 42
Jeegar Patel Avatar answered Sep 19 '22 01:09

Jeegar Patel