60

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Manny
  • 639
  • 2
  • 6
  • 7
  • 1
    What is the compiler that you are using? For gcc you should take a look at the -I flag (see the manual page). For other compilers check out the documentation. – Ed Heal Sep 28 '11 at 09:59

4 Answers4

64

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

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • 5
    Please note that this is -- in theory -- platform/compiler specific. "The named source file is searched for in an implementation-defined manner." (ISO/IEC 9899 on '#include "file"') – undur_gongor Sep 28 '11 at 10:55
35

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
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
11

If you work on a Makefile project or simply run your code from command line, use

gcc -IC main.c

where -I option adds your C directory to the list of directories to be searched for header files, so you'll be able to use #include "structures.h"anywhere in your project.

Timandi Vlad
  • 111
  • 1
  • 4
1

If you want to use the command line argument then you can give gcc -idirafter ../b/ main.c

then you don't have to do any thing inside your program.

Vishwajeet Vishu
  • 492
  • 3
  • 16