Aquí un ejemplo completo de makefile:
makefile
TARGET = prog
$(TARGET): main.o lib.a
gcc $^ -o $@
main.o: main.c
gcc -c $< -o $@
lib.a: lib1.o lib2.o
ar rcs $@ $^
lib1.o: lib1.c lib1.h
gcc -c -o $@ $<
lib2.o: lib2.c lib2.h
gcc -c -o $@ $<
clean:
rm -f *.o *.a $(TARGET)
explicando el archivo MAKE:
target: prerequisites - el jefe de la regla
$@ - significa el objetivo
$^ - significa todos los requisitos previos
$< - significa solo el primer requisito previo
ar- una herramienta de Linux para crear, modificar y extraer archivos, consulte las páginas de manual para obtener más información . Las opciones en este caso significan:
r - reemplazar archivos existentes dentro del archivo
c - crear un archivo si aún no existe
s - Crear un índice de archivo de objeto en el archivo
Para concluir : la biblioteca estática en Linux no es más que un archivo de archivos de objetos.
main.c usando la lib
#include <stdio.h>
#include "lib.h"
int main ( void )
{
fun1(10);
fun2(10);
return 0;
}
lib.h el encabezado principal de libs
#ifndef LIB_H_INCLUDED
#define LIB_H_INCLUDED
#include "lib1.h"
#include "lib2.h"
#endif
lib1.c primera fuente lib
#include "lib1.h"
#include <stdio.h>
void fun1 ( int x )
{
printf("%i\n",x);
}
lib1.h el encabezado correspondiente
#ifndef LIB1_H_INCLUDED
#define LIB1_H_INCLUDED
#ifdef __cplusplus
extern “C” {
#endif
void fun1 ( int x );
#ifdef __cplusplus
}
#endif
#endif /* LIB1_H_INCLUDED */
segunda fuente lib2.c lib
#include "lib2.h"
#include <stdio.h>
void fun2 ( int x )
{
printf("%i\n",2*x);
}
lib2.h el encabezado correspondiente
#ifndef LIB2_H_INCLUDED
#define LIB2_H_INCLUDED
#ifdef __cplusplus
extern “C” {
#endif
void fun2 ( int x );
#ifdef __cplusplus
}
#endif
#endif /* LIB2_H_INCLUDED */