Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile library on C using GCC?

I made a library with the files pila.h and pila.c. I compile the file pila.c with gcc pila.c -c and this library works fine. I have tested it.

Then I made another library. This library has the files pila_funciones_extra.h and pila_funciones_extra.c. In this library I need to include the first library. In the file pila_funciones_extra.h I put the next line to include it:

#include "pila.h"

and in the file pila_funciones_extra.c I put the next line:

#include "pila_funciones_extra.h"

as it has to be.

But when I try to compile the file pila_funciones_extra.c the compiler doensn't recognize the inclusion of the library pila. It says that the functions, structures, constants and macros that are defined in the library pila haven't been defined.

I tried to compile it with gcc pila_funciones_extra.c -c and gcc pila_funciones_extra.c -c pila.o but it doesn't work.

I made sure that all the files are in the same folder.

I'm working on Ubuntu.

Can anyone tell me the right way to compile it?

like image 748
Adrian Avatar asked Feb 17 '13 08:02

Adrian


2 Answers

First, always take the habit to compile with -Wall (and perhaps even also -Wextra to get even more warnings) option to gcc; it gives you almost all warnings, and you should improve your code till no warnings are given.

Then you often want to be able to debug your code, so also pass -g to gcc. Once you are confident with your code you could ask gcc to produce optimized machine code with -O2. Learn to use the gdb debugger.

So compile your first library, assuming its source files first1.c and first2.c are in FirstLib/ directory, with e.g.

cd FirstLib/
gcc -Wall -g -c first1.c -o first1.o
gcc -Wall -g -c first2.c -o first2.o

At this point, you should use a Makefile and learn how to use make, in particular because you want to get your libfirst.a with

ar ruv libfirst.a first1.o first2.o
ranlib libfirst.a

Then you can pass -L../FirstLib -lfirst as the last options to the gcc command compiling and linking your program using your libfirst.a

Then compile your second library by having your Makefile in directory SecondLib/ which very probably should contain

# in SecondLib/Makefile
CC=gcc
CFLAGS= -I../FirstLib/ -Wall -g
## add your other stuff for make

etc etc. You really want to learn how to use make and write your own Makefile-s so take time to read the GNU make documentation.

You may want to pass -H to gcc to have it tell you all the included files, and you may want to also use remake (in addition & replacement of make) to debug your more complex Makefile-s (notably by running remake -x). Here is an example of Makefile; you'll find many others!

Read also the Program Library Howto.

like image 124
Basile Starynkevitch Avatar answered Sep 28 '22 05:09

Basile Starynkevitch


For a library composed of many files you can first compile then separately and then do this:

ar ruv mylib.a pila_funciones_extra.o pila.o
ranlib mylib.a

This command causes the library to be created if it does not already exist. If it does, the .o files are updated (or added to this library). The ranlib is used to randomize the library in a way that is useful for the loader.

When you use this library, you do:

gcc -o myapp myapp.c mylib.a
like image 25
kdmin Avatar answered Sep 28 '22 04:09

kdmin