2010-04-29 28 views
99

Tengo una pregunta: Cómo compilar una biblioteca estática en Linux con gcc, es decir, necesito compilar mi código fuente en un archivo llamado out.a. ¿Es suficiente simplemente compilar con el comando gcc -o out.a out.c? No estoy muy familiarizado con gcc, espero que alguien pueda darme una mano.¿Cómo compilar una biblioteca estática en Linux?

Respuesta

154

Ver Creating a shared and static library with the gnu compiler [gcc]

gcc -c -o out.o out.c 

-c medio para crear un archivo de objeto intermediario, en lugar de un archivo ejecutable.

ar rcs libout.a out.o 

Esto crea la biblioteca estática. r significa insertar con reemplazo, c significa crear un nuevo archivo y s significa escribir un índice. Como siempre, vea el man page para más información.

9

Genere los archivos de objeto con gcc, luego use ar para agruparlos en una biblioteca estática.

50

Aquí un ejemplo completo makefile:

makefile

TARGET = prog 

$(TARGET): main.o lib.a 
    gcc $^ -o [email protected] 

main.o: main.c 
    gcc -c $< -o [email protected] 

lib.a: lib1.o lib2.o 
    ar rcs [email protected] $^ 

lib1.o: lib1.c lib1.h 
    gcc -c -o [email protected] $< 

lib2.o: lib2.c lib2.h 
    gcc -c -o [email protected] $< 

clean: 
    rm -f *.o *.a $(TARGET) 

explicar el makefile:

  • target: prerequisites - la cabeza regla
  • [email protected] - significa que el objetivo
  • $^ - significa todos los requisitos previos
  • $< - significa simplemente el primer requisito
  • ar - una herramienta de Linux para crear, modificar y extraer de los archivos see the man pages for further information. Las opciones en este caso se refiere a:
    • r - reemplazar los archivos existentes dentro del archivo
    • c - crear un archivo si no está ya existentes
    • s - crear un índice objeto-archivo en el archivo

Para concluir: La biblioteca estática en Linux no es más que un archivo de archivos de objeto.

main.cusando el lib

#include <stdio.h> 

#include "lib.h" 

int main (void) 
{ 
    fun1(10); 
    fun2(10); 
    return 0; 
} 

lib.hlos libs de cabecera principal

#ifndef LIB_H_INCLUDED 
#define LIB_H_INCLUDED 

#include "lib1.h" 
#include "lib2.h" 

#endif 

lib1.cprimera fuente lib

#include "lib1.h" 

#include <stdio.h> 

void fun1 (int x) 
{ 
    printf("%i\n",x); 
} 

lib1.hla cabecera 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 */ 

lib2.csegunda fuente lib

#include "lib2.h" 

#include <stdio.h> 

void fun2 (int x) 
{ 
    printf("%i\n",2*x); 
} 

lib2.hel 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 */ 
+1

Gracias por la explicación. –

+4

las mejores respuestas son aquellas con ejemplos, gran trabajo :) – Youda008

+0

hubiera ayudado a señalar lo que hacen los comandos y lo que pretenden lograr. especialmente en este caso el 'ar' necesita explicación, ya que es la clave para crear la biblioteca estática. – Joost

Cuestiones relacionadas