Error: no such file or directory - C - c

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?

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.

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"

Related

gcc prints no such header file when compile it from upper level folder

I have this command here:
gcc -MD -fno-builtin -nostdinc -fno-stack-protector -Os -g -m32 -I. -c -o boot1lib.o
boot1lib.c
It runs fine if I run this in the folder where boot1lib.o and boot1lib.c located. But when I tried to run it from the upper folder i.e. ./boot/boot1/boot1lib.c
It will shows:
./boot/boot1/boot1lib.c:1:10: fatal error: boot1lib.h: No such file or directory #include <boot1lib.h>
How do I modify the parameters to fix this issue? I am trying to build a makefile in the root folder so I don't have to copy and paste the command every time I tried to compile.
With GCC, #include <file> looks for files only in configured system directories, including those added with switches. #include "file" looks in the directory of the source file it is in.

GCC error No such file or directory when including a separate file

When I try to compile including the file stack.h my GCC gives me the error "No such file or directory"
gcc -std=c99 -Wall -o Ou1 ou1.c -I C:\Users\NAME\Documents\C programmering\DOA\stack.h
The code looks like this:
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
I've tried to change its folder and changing the .h to .c and the .c to .h.
First, -I expects a path to a directory. Secondly, your path contains spaces, so you should enclose it in quotes to make sure it's not wrongly treated as two different arguments:
gcc -std=c99 -Wall -o Ou1 ou1.c -I "C:\Users\NAME\Documents\C programmering\DOA"

gcc doesn't find header file in static library I made

I made a static library.
This not have error, library file too.
So, I tried use that library.
gcc -o hash_gen main.c -L../ -lhashbundle
Library file exist in that directory ../, library file name is libhashbundle.a.
So, I thought not have problem in this command.
but I tried compile with gcc, but gcc print this error.
main.c:4:10: fatal error: 'hash.h' file not found
#include "hash.h"
^
I don't understand. I made library make, and this is Makefile
all : libhashbundle.a
libhashbundle.a : hash.o
ar rscv libhashbundle.a hash.o
hash.o : src/hash.c
gcc -c src/hash.c
clean:
rm -rf hash.o
I thought this code many times, but I didn't found error.
and this is directory tree
tree
Makefile
libhashbundle.a
|src
|hash.c
|hash.h
|test
|main.c
So, I ask to you.
How could solve this problem?
You only specified a library search path (-L).
If you want a header search path, you need to use -I.
gcc -o hash_gen main.c -I.. -L.. -lhashbundle
The problem is the fact that you're running your build from your "root" directory, not from /src/. So when the compiler sees #include "hash.h" it will try to open the file in the current directory, which will be / rather than /src/ (where the files are).
To fix this, just add your source directory to the include search paths using -Isrc:
gcc -o hash_gen main.c -Isrc -lhashbundle
Note that I omitted -L since the library file is in your current working directory and should be found there anyway.

GCC unable to find header file in a included library

I am trying to include a library file named libmathematica.a in gcc so it gets linked in the executable example.
I attempt to do this with gcc main.c libmathematica.a -o example
Note: I have to do this with gcc, as ld won't link it properly with the correct system libraries
But I get: fatal error: mathematica.h: No such file or directory ,which is odd because mathematica.h is in the library.
Can you help?
A header file cannot be in the library. It has to be present at a certain location and you have to specify that location with the -I compiler flag:
gcc -I/path/to/mathematica/include main.c libmathematica.a -o example
If the header file is in the directory where the main.c is or in a subdirectory, then be sure that you use quotes and not angle brackets in the #include directive.
The issue would be in your source file. If the mathematica.h is in the system includes directory then you would use #include <mathematica.h> and if it was in some local directory then you would use something like #include "libs/mathematica.h".
Try adding to the gcc call - an option like
-I/Full/Path/To/The/Directory/Where/the/desired/header/resides
For example:
gcc -I/usr/include/mathematica -lmathematica -o example main.c

C header files and dynamic linking error

I have created a dynamically linked library. The only problem I have is that my main program does not recognize my header file. The header file is in a separate folder from my main program. I have tried #include "myheader.h" as well as #include "/folder/dir/myheader.h"
Here is what my .h consist of
extern int afunction(int,int);
extern int afunction(int,int);
So far this code works
gcc -fPIC -c filename1.c
gcc -fPIC -c filename2.c
gcc -shared -o libMylib.so filename1.o filename2.o
I then copy the lib to /usr/local/lib, and then
gcc main.c -L. -lMylib -o exeName -ldl
and I get
" myheader.h : no such file or directory.
Here is my directory structure:
directory1 ----------------folder1(main program.c)
directory1 ----------------folder2(myheader.h, along with functions for the pgm)
A push in the right direction would help, as I have written all my code and I am just at the last phase.
You need gcc ... -I<some directory to myheader.h>. That will enable the compiler to find the header file.
You can put your lib header files in the same folder with your current program.
or like #Ed Heal said.. adding -I<path> for include header folder.

Resources