4

After interpreting this comment,

/***************** arrayImpl.c **************/

#include"list/list.h"

#if defined(ARRAY)
 ....
#endif

I wrote #include"list/list.h" in ./Computing/list/arrayImpl.c for testing Computing/list ADT using Computing/testList.c program, shown here.

But list/list.h could not be found by list/arrayImpl.c, as shown below,

PC ~/code_practice/Computing
$ gcc -Wall -g -DARRAY ./list/*.c testList.c -o testList
./list/arrayImpl.c:3:22: fatal error: list/list.h: No such file or directory
compilation terminated.
./list/linkedListImpl.c:3:22: fatal error: list/list.h: No such file or directory
compilation terminated.

How do I understand this error, after following that comment? Did I mis-interpret?

Community
  • 1
  • 1
overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

5

list.h is in the same directory as the c files which include it. When you do

#include "list/list.h"

the compiler tries to find the file in include path + /list. For instance, it will look for list/list/list.h which doesn't exist.

So what would work would be changing to #include "list.h"

OR

add current directory to the command line using -I. so list/list.h is in include path.

gcc -Wall -g -I. -DARRAY ./list/*.c testList.c -o testList

From gcc search path documentation

-I. -I- is not the same as no -I options at all, and does not cause the same behavior for ‘<>’ includes that ‘""’ includes get with no special options. -I. searches the compiler's current working directory for header files. That may or may not be the same as the directory containing the current file.

It's not mentioned anywhere that include path contains the current directory, from which gcc was started.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I did many tests and the comment is wrong. Check my edit, the documentation doesn't talk about current directory. – Jean-François Fabre Dec 23 '16 at 20:36
  • When gcc doc says: *That may or may not be the same as the directory containing the current file.* It mean, the directory `list` containing with current file `arrayImpl.c` in this context. Is that correct? – overexchange Dec 23 '16 at 20:37
  • yes, exactly. The current compiled file dir is automatically added to the compiler search path for this compilation only, unless file is included using `<>`, which tells to not include current compiled file dir. – Jean-François Fabre Dec 23 '16 at 20:40
1

You need to add include file directory "list".

https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

gcc -Wall -g -DARRAY ./list/*.c testList.c -I list -o testList

And you must remove "list" from "#include "list/list.h". Because when you write that you tell to the compiler to search in all include directory a file "list/list.h". But "list.h" is in "list". So "list" is not necessary.

#include "list.h"

You could do that but it's ugly

#include "../list/list.h"
Stargateur
  • 24,473
  • 8
  • 65
  • 91