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?
For a library composed of many files you can first compile then separately and then do this:
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:
First, always take the habit to compile with
-Wall
(and perhaps even also-Wextra
to get even more warnings) option togcc
; 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
togcc
. Once you are confident with your code you could askgcc
to produce optimized machine code with-O2
. Learn to use thegdb
debugger.So compile your first library, assuming its source files
first1.c
andfirst2.c
are inFirstLib/
directory, with e.g.At this point, you should use a
Makefile
and learn how to usemake
, in particular because you want to get yourlibfirst.a
withThen you can pass
-L../FirstLib -lfirst
as the last options to thegcc
command compiling and linking your program using yourlibfirst.a
Then compile your second library by having your
Makefile
in directorySecondLib/
which very probably should containetc etc. You really want to learn how to use
make
and write your ownMakefile
-s so take time to read the GNU make documentation.You may want to pass
-H
togcc
to have it tell you all the included files, and you may want to also use remake (in addition & replacement ofmake
) to debug your more complexMakefile
-s (notably by runningremake -x
). Here is an example ofMakefile
; you'll find many others!Read also the Program Library Howto.