3

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

rra
  • 3,807
  • 18
  • 29
moku
  • 31
  • 1
  • 3
  • 2
    I set up the structure you describe above and tried it with gcc 4.7.2, and it worked as expected. So I don't think there's anything fundamentally wrong with what you're trying to do, but I'm not sure why it's not working for you. – rra Mar 24 '13 at 17:55
  • This works just fine for me as well. Have you tried defining the environmental variable at the command line? I can't think of a reason for a shell to not execute /etc/profile, but how about trying `export PATH=\BBB; gcc -c A.c B.c` – brunobeltran0 Mar 24 '13 at 17:56
  • er, i mistake ur meaning, u mean 'export CPATH=\BBB', not 'export PATH=\BBB' right? – moku Mar 24 '13 at 18:16
  • What do you get if you run `export | grep C_INCLUDE`? – teppic Mar 24 '13 at 18:27
  • it shows the right place i included, 'declare -x C_INCLUDE_PATH="/home/workspace/routine/include'. – moku Mar 24 '13 at 18:29
  • And B.h is definitely in that directory? – teppic Mar 24 '13 at 18:31
  • yeah, as i said, '#gcc -I /BBB A.c B.c -o exeobj' worked... – moku Mar 24 '13 at 18:33

1 Answers1

0

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

Quentin Perez
  • 2,833
  • 20
  • 22
  • 1
    That actually shouldn't make a difference, I think. The difference between "" and <> is that "" also searches the current directory first, but then they both fall back to the same search algorithm after that. I've never tried to use C_INCLUDE_DIR before, though.... – rra Mar 24 '13 at 17:29
  • The GCC manual [backs up](http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html#Search-Path) rra's claim. – gspr Mar 24 '13 at 17:30
  • @Quentin Perez: The section 6.10.2 you cite explicitly says that in the case `#include "foo.h"` the file is "searched for in an implementation-defined manner". – gspr Mar 24 '13 at 17:35
  • After test with the variable C_INCLUDE_PATH my file.h is not found too. – Quentin Perez Mar 24 '13 at 17:47
  • 2
    The implementation-defined manner in which gcc handles "" in #include is to search the same directory as the C source file first, and then fall back on the normal <> path. – rra Mar 24 '13 at 17:51
  • @QuentinPerez Yeah, it works for me too, but so does the situation in the original question, so I'm not sure what's going wrong for Takechiyocn. – rra Mar 24 '13 at 17:56
  • well, the directory specified by environment variable "C_INCLUDE_PATH" is searched after current directory , and last is normal<> path, why it is OK on rra but NG on Quentin Perez, factors on system environment are possible? – moku Mar 24 '13 at 18:03