Header search paths with gcc using C_INCLUDE_PATH

Hidenori Kohata picture Hidenori Kohata · Mar 24, 2013 · Viewed 8.1k times · Source

i'm confused about the following include files(with GCC)

i've A.c and B.c in folder AAA

and B.h in folder BBB

in A.c:

#include <stdio.h>
#include "B.h"

main()
{
    errPrint();
}

in B.c:

#include <stdio.h>
#include "B.h"
void errPrint(void)   
{
    printf("err info\n");
}

in B.h:

#ifndef _B_H
#define _B_H
void errPrint(void);
#endif

now i run the command:

#gcc -I /BBB A.c B.c -o exeobj

it's OK. but it seems a little boring that i have to use "-I" to specify header when in other folder. so i edit my "/etc/profile" file and added

C_INCLUDE_PATH=/BBB  
export C_INCLUDE_PATH

to specify the header folder, then

echo $C_INCLUDE_PATH

it shows the right route. but when i compile:

#gcc -c A.c B.c

error shows:

error: B.h: No such file or directory

i don't know where went wrong, anybody have clues about it, any suggestions are weclome.

note: i'm a newbie and can't use Makefile yet...

Answer

Quentin Perez picture Quentin Perez · Mar 24, 2013

in A.c:

#include <stdio.h>
#include <B.h>

main()
{
    errPrint();
}

in B.c:

#include <stdio.h>
#include <B.h>
void errPrint(void)   
{
    printf("err info\n");
}

If want to use #include "file.h" you gotta specified path example: "/BBB/B.h"

For more info you can read In the C standard section 6.10.2, paragraphs 2 to 4.

EDIT: After test. try it please.

echo -e "C_INCLUDE_PATH=/BBB\nexport C_INCLUDE_PATH" >> ~/.bash_profile

source ~/.bash_profile

and now

gcc A.c B.c

Good Lucks :)