问题
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?
回答1:
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 exit.
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.
回答2:
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"
来源:https://stackoverflow.com/questions/41307381/error-no-such-file-or-directory-c