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

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.

Related

Using a static library in C

I found a useful library on github for my project, after building this later I tried to use some predefined function on it. I couldn't compile my project because there is some header file missing like this one :
In file included from main.c:2:0:
ptask.h:11:19: fatal error: ptime.h: No such file or directory
I compiled my project using this command :
gcc main.c -L. -lptask
This is all the files in project folder :
libptask.a main.c ptask.h
This is the library content:
$ ar -t libptask.a
pbarrier.c.o
pmutex.c.o
ptask.c.o
ptime.c.o
rtmode.c.o
tstat.c.o
libdl.c.o
dle_timer.c.o
calibrate.c.o
Do I need to add all the headers of this files or just link the lib when compiling ?
Your main.c #include-s ptask.h which in turn #include-s ptime.h. Having compiled static libs alone is not enough (that's the linker's job), you still need to have all used header files (which is the compiler's job), both the ones you use and their dependencies, recursively applicable.
Normally you need to be sure that the header files are in your "include path", something that a lot of compilers define with -I as a command-line option. You'll need to include the source directory of that library, or if it has a make install option, then the place where they got installed.
regarding:
gcc main.c -L. -lptask
this is performing the compile step and the link step in one command.
It is also not enabling the warnings, which should always be enabled during the compile step.
Suggest something similar to the following to compile
gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -g -c main.c -o main.o -I.
and when you have fixed all the warnings, then use something similar to the following to link
gcc main.o -o main -L. -lptask

Error: no such file or directory - 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"

GCC shared library linked but header file in library not found

So I have some trouble using shared libraries in combination with GCC.
I'm quite new to this so that's why I came to ask it here. I was unable to find a question on stack overflow that helps me with mine (maybe i overlooked one). But first something about my setup and what I'm trying to accomplish.
So I have these two libraries that are development parallel to each other. One is named liblinkedlist, which contains the linkedlist implementation in C and the other is named libgraph, containing a graph implementation in C. Both are put in the following folder structure:
<root>
+---graph_lib(folder)
+---build(folder)
+---src(folder)
+---makefile(file)
+---linkedlist_lib
+---build(folder)
+---src(folder)
+---makefile(file)
Each src folder contains the source files (linkedlist.h and .c for liblinkedlist and graph.h and .c for libgraph)
In each build folder the .o and .so files created from the header files are stored.
Now the problem is that I want to use the liblinkedlist.so in /linkedlist_lib/build/ in my graph library. While compiling the libraries everything seems to go well. But when I try to use it in combination with including a header file (which is inside the linkedlist library), I get the message that it could not be found.
My compile commands are the following:
For the liblinkedlist:
gcc -fpic -c src/linkedlist.c -o build/linkedlist.o
gcc -shared -o build/liblinkedlist.so build/linkedlist.o
And for the libgraph wich uses the liblinkedlist:
gcc -fpic -c src/libgraph.c -o build/libgraph.o
gcc -fpic -c src/graph.c -o build/graph.o
gcc -L../linkedlist_lib/build/ -o build/libgraph build/libgraph.o build/graph.o -llinkedlist
These are the command and errors I get when using the header file:
gcc -fpic -c src/libgraph.c -o build/libgraph.o
In file included from src/libgraph.c:2:0:
src/graph.h:4:24: fatal error: linkedlist.h: File or folder does not exist
#include "linkedlist.h"
^
compilation terminated.
make: *** [build/libgraph.o] Fout 1
Any ideas on how to fix this problem, am I doing something wrong here?
Thanks in advance

Fatal error: modbus.h: No such file or directory

I'm expecting a lot of difficulties to make my program working with the library libmodbus on Linux.
I've installed libmodbus with the command sudo make install and after make but the problem is when I want to link the library in my C program.
My Makefile for now is like:
all: test
test: main.o com.o
gcc main.o com.o -o test
main.o: main.c
gcc -c main.c -o main.o
com.o: com.c
gcc -c com.c -Wl,-rpath=/usr/local/lib -Wl,LIBDIR -o com.o
clean:
rm -rf *o test
In my file com.c I include the file modbus.h like this:
#include <modbus.h>
And I always get the error:
fatal error: modbus.h: No such file or directory.
If it can help when I did make install, the code return me this:
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution
add LIBDIR to the `LD_RUN_PATH' environment variable during linking
use the `-Wl,-rpath -Wl,LIBDIR' linker flag
have your system administrator add LIBDIR to `/etc/ld.so.conf'
It seems like the modbus.h is not in the standard include directory.
You should to add the -I/<includes_path> flag to gcc options.
I suppose here:
gcc -I/<include_dir_path> -c com.c -Wl,-rpath=/usr/local/lib -Wl,LIBDIR -o com.o

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