C header files and dynamic linking error - c

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.

Related

How to use gcc correctly from the ubuntu terminal?

I'm try to compile a .c file from the terminal using gcc. The file includes a personal library where a function is defined. This library.h and the .c file are in the same directory.
I get the following message error
undefined reference to `function'"
Should I use another argument as:
gcc -o nameoutput filename
or should I place the library.h in another directory?
Assuming you have library.c, library.h and main.c in your current working directory:
$ gcc -Wall main.c library.c -o my_program
and then to run it:
$ ./my_program
"Undefined reference" means that the linker can't find the object file containing the compiled body of function; it doesn't have anything to do with the .h file.
I sounds like you have a situation where library.h and library.c are in one directory, and main.c is in a different directory. If that's the case, then your command line will need to look something like this:
gcc -o program -I /path/to/library main.c /path/to/library/library.c
-I /path/to/library means that gcc will look for .h files in that path as well as the standard include paths. That also allows you to write
#include "library.h"
instead of
#include "/path/to/library/library.h"
in any code that needs it.

header.h: No such file or directory even though source and header are in same directory

I have made a header and a source but I don't know how to link them up. I looked it up on the web but the commands provided didn't work (or I wouldn't be here :) ).
To compile it (if you use GCC):
Header:
$ gcc -c whatever.h -o whatever.o
Source:
$ gcc -c sample.c -o sample.o
To link the files to create an executable file:
$ gcc sample.o whatever.o -o sample
What did I do wrong. I am using geany for writing (compile error is here) but the commands are executed on a terminal in the same directory. can anybody give me the build commands for geany so whenever I want to include a header I can just compile and run?
Good and the right way would be to
sample.c
#include "header.h"
and compile
gcc sample.c -o ob
Thumb Rule:
header files [.h] are for #includeing
source files [.c] are for compiling and linking together to create the executable.
Once you've #included your header file in a .c file, there's no need to compile the header file and produce an object file.
FYI, you can check the effect of #include-ing the header file by running
gcc -E sample.c
and hope you'll understand why you need not compile and link the header file separately.
EDIT:
if you have a sample.c and whatever.h, to produce and run the binary, simply do
#include "whatever.h" in the top of sample.c
gcc -o sample sample.c
./sample
if you include header file by:
#include <header.h>
it will give this error.
Instead you can write as given below:
#include "header.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

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.

How to write makefile for C program in UNIX

I have a program test.c which needs to include a header file common.h. I am new to writing make files and so far I have the right make file for just test.c (it uses openssl):
INC=/usr/local/ssl/include/
LIB=/usr/local/ssl/lib/
all:
gcc -I$(INC) -L$(LIB) -o test test.c -lcrypto -ldl
How do I edit the above file to also compile common.h ?
Thanks.
You don't compile header files directly. They will be included by #include in the C files. If common.h is located in the same directory as the make file, you won't need to add anything. Otherwise, you might want to edit the makefile to add other folders that include header files:
INC=-I/usr/local/ssl/include/ \
-I/path/to/another/folder/to/include
LIB=/usr/local/ssl/lib/
all:
gcc $(INC) -L$(LIB) -o test test.c -lcrypto -ldl

Resources