185

I have a question: How to compile a static library in Linux with gcc, i.e. I need to compile my source code into a file named out.a. Is it sufficient to simply compile with the command gcc -o out.a out.c? I'm not quite familiar with gcc, hope anyone can give me a hand.

red0ct
  • 4,840
  • 3
  • 17
  • 44
Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83

3 Answers3

270

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

gcc -c -o out.o out.c

-c means to create an intermediary object file, rather than an executable.

ar rcs libout.a out.o
 

This creates the static library. r means to insert with replacement, c means to create a new archive, and s means to write an index. As always, see the man page for more info.

AJM
  • 1,317
  • 2
  • 15
  • 30
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
119

Here a full makefile example:

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)

explaining the makefile:

  • target: prerequisites - the rule head
  • $@ - means the target
  • $^ - means all prerequisites
  • $< - means just the first prerequisite
  • ar - a Linux tool to create, modify, and extract from archives see the man pages for further information. The options in this case mean:
    • r - replace files existing inside the archive
    • c - create a archive if not already existent
    • s - create an object-file index into the archive

To conclude: The static library under Linux is nothing more than a archive of object files.

main.c using the lib

#include <stdio.h>

#include "lib.h"

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

lib.h the libs main header

#ifndef LIB_H_INCLUDED
#define LIB_H_INCLUDED

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

#endif

lib1.c first lib source

#include "lib1.h"

#include <stdio.h>

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

lib1.h the corresponding header

#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.c second lib source

#include "lib2.h"

#include <stdio.h>

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

lib2.h the corresponding header

#ifndef LIB2_H_INCLUDED
#define LIB2_H_INCLUDED

#ifdef __cplusplus
   extern “C” {
#endif

void fun2 ( int x );

#ifdef __cplusplus
   }
#endif

#endif /* LIB2_H_INCLUDED */
Alex44
  • 3,597
  • 7
  • 39
  • 56
  • 1
    it would have helped to point out what the commands do, and what they intend to achieve. especially in this case the `ar` needs explanation, as it is the key to creating the static library. – Joost Jul 01 '16 at 13:26
  • 1
    The `ar` program creates, modifies, and extracts from archives, which are a single files holding a collection of other files in a structure that makes it possible to retrieve the original individual files. `ar` creates an index to the symbols defined in relocatable object modules in the archive when you specify the modifier s. (see `man ar`) – Alex44 Jul 01 '16 at 18:10
  • 2
    please add following lines to your header to support ```c++``` compiler: ```#ifdef __cplusplus extern "C" { #endif . . . #ifdef __cplusplus } #endif``` – Behrouz.M Aug 18 '16 at 04:27
  • This is a brilliant answer, thank you! – Ilia Sidorenko Dec 30 '21 at 12:34
14

Generate the object files with gcc, then use ar to bundle them into a static library.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358