How to add a custom library to GCC while compliling? - c

i have downloaded a C library from the net with the name libsal.a, to access the APIs in that library i include a header file by name #include in my main.c. i use the following command to compile it :
gcc -L /home/traana/Desktop/opensal-1.0.0/libsal.a main.c
but get the following error when i compile
main.c:3:17: fatal error: sal.h: No such file or directory
compilation terminated.
How do I do this correctly?

When compiling your source, it's not the library, but the include files that are relevant; so you'll need to tell gcc where those are, something like
gcc -c -I/home/traana/Desktop/opensal-1.0.0/include main.c
and then link the application with something like
gcc -L/home/traana/Desktop/opensal-1.0.0 -lsal main.o

I believe that there are three arguments that you will need to set:
-I <include directory>
-L <library directory>
-l <library name>
The include directory will be location of the header files. The library directory will be the location of the library (.a or .so), and the library name will be the name of the library file, without the leading 'lib' prefix or its extension (i.e. -lsal rather than -l libsal.a ).

if the *.h are in /home/traana/Desktop/opensal-1.0.0
try
gcc -I /home/traana/Desktop/opensal-1.0.0 -L /home/traana/Desktop/opensal-1.0.0 main.c -lsal

Related

How to let gcc compiler know where a certain file is

I'm trying to compile my C code but I need to tell the GCC compiler where two file are.
The two files are located here
/usr/local/ssl/include/
/usr/local/ssl/lib/
I want to add this to my gcc -o file file.c so that my program can work.
In gcc, the -I option is used for adding a directory to the set of directories to search for header files, and the -L option is used for adding a directory to the set of directories to search for libraries. Since you're not explicitly linking in any libraries, you shouldn't need the -L option in this case.
gcc -I/usr/local/ssl/include -o file file.c
If you were linking in libraries, something like the following format should work, assuming that file.c calls a function in libmyLib.a:
gcc -I/usr/local/ssl/include -o file file.c -L/path/to/my/library -lmyLib
See this question for more details regarding library linking order.

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.

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.

command to compile c files with .a files

I have several .c files and one .a object file. What command with gcc should I use to compile them to one exe file? If we use a makefile, how will it look like?
For simple cases you can probably do this:
gcc -o maybe.exe useful.a something.c
Makefiles for non-trivial projects usually first invoke gcc to compile each .c file to a .o object.
gcc -c something.c
Then they invoke the linker (these days often using gcc as a wrapper for it) with a list of .o and .a files to link into an output executable.
gcc -o maybe.exe useful.a something.o
Note also that for most installed libraries, it's typical not to explicitly specify the .a file but instead to say -lhandy which would be short for "try to find something called libhandy.a in the configured (or specified with -L) search directories"
*.a is a static library and not dynamic (*.dll in windows and *.so in linux)
gcc -L<here comes the library path> -l<library name>
for example for the file you have libname.a in the current path you should use:
gcc *.c -L. -lname -o myprogram.o
from the man (put man gcc in the shell command prompt)
You can mix options and other arguments. For the most part, the order
you use doesn't matter. Order does matter when you use several options
of the same kind; for example, if you specify -L more than once, the
directories are searched in the order specified. Also, the placement
of the -l option is significant.
The .a file is a library, already compiled. You compile your .c file to a .o, then you use the linker to link your .o with the .a to produce an executable.

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

Resources