How to Link Static Library in C/C++ using GCC compiler?


Unlike Dynamic Link Library (DLL), the static library are pre-compiled and linked to the binary executables. Once built into the final executables, the static library cannot be shared among the others. The static library is part of the binary code and that means it is loaded as the program starts i.e. DLL files can be loaded at runtime whenever needed.

static-library-linked-into-executable How to Link Static Library in C/C++ using GCC compiler? c / c++ gcc compiler programming languages tutorial

static-library-linked-into-executable

How to Create Static Library in C/C++?

It turns out that it is very simple to create static library in C/C++. A static library is basically an archive (like a zip file) of object files, which are compiled from the *.c/*.cpp source code. Each source code contains the exported functions. For example, let’s create two source files: test1.c and test2.c that contains two functions respectively.

1
2
3
4
// test1.c
int test1(int a) {
        return a + 1;
}
// test1.c
int test1(int a) {
        return a + 1;
}

There is no need to define entry main as these are not programs but function libraries.

1
2
3
4
// test2.c
void test2(int *c) {
        *c = *c + 1;
}
// test2.c
void test2(int *c) {
        *c = *c + 1;
}

Now, we can compile these into *.o object files.

1
gcc -c test1.c test2.c
gcc -c test1.c test2.c

The -c switch means: Compile and assemble, but do not link. We will have two files: test1.o and test2.o and they are object files:

1
2
3
$ file *.o
test1.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
test2.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
$ file *.o
test1.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
test2.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Now, as mentioned above, we can use ar to put these object files together into a single static library.

1
2
3
4
$ ar rsv testlib.a test1.o test2.o
ar: creating testlib.a
a - test1.o
a - test2.o
$ ar rsv testlib.a test1.o test2.o
ar: creating testlib.a
a - test1.o
a - test2.o

The switch for ar command: r – replace, s – create an archive, v – verbose. Now the testlib.a contains test1.o and test2.o which we can verify by:

1
2
3
4
5
$ file testlib.a
testlib.a: current ar archive
$ ar -t testlib.a
test1.o
test2.o
$ file testlib.a
testlib.a: current ar archive
$ ar -t testlib.a
test1.o
test2.o

How to Use Static Library in C/C++?

Let’s create a test file and invoke these two methods from the static library.

1
2
3
4
5
6
7
8
9
#include <stdio.h>
 
int main() {
        int c = 3;
        test2(&c);
        printf("%d\n", c);
        printf("%d\n", test1(c));
        return 0;
}
#include <stdio.h>

int main() {
        int c = 3;
        test2(&c);
        printf("%d\n", c);
        printf("%d\n", test1(c));
        return 0;
}

To compile it, use the following command (pass the static library testlib.a as the source file, so gcc compiler knows to link it later.

1
gcc -o test.out test.c testlib.a
gcc -o test.out test.c testlib.a

Alternatively, you could use the explicity linking options to link the static library (-L switch specifies the static library path and -l followed by the name of the static library):

1
gcc -o test.out test.c -L. -ltestlib
gcc -o test.out test.c -L. -ltestlib

However, it will output warnings:

1
2
3
4
5
6
test.c: In function ‘main’:
warning: implicit declaration of function ‘test2’ [-Wimplicit-function-declaration]
  test2(&c);
  ^
warning: implicit declaration of function ‘test1’ [-Wimplicit-function-declaration]
  printf("%d\n", test1(c));
test.c: In function ‘main’:
warning: implicit declaration of function ‘test2’ [-Wimplicit-function-declaration]
  test2(&c);
  ^
warning: implicit declaration of function ‘test1’ [-Wimplicit-function-declaration]
  printf("%d\n", test1(c));

However, the compiler still generates the binary code and you can run it:

1
2
3
$ ./test.out
4
5
$ ./test.out
4
5

The warnings are to warn you that the compiler is not sure the function declarations of test1 and test2. To make the gcc compiler happy, you need to declare the two functions before usage:

1
2
int test1(int c);
void test2(int *c);
int test1(int c);
void test2(int *c);

That is why the static library is distributed with a function declaration header files *.h, so that you know how to invoke them and the compiler takes care of them e.g. linking *.a static libraries into your executables.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
755 words
Last Post: How to Get Minimum Moves to Equal Array Elements?
Next Post: How to Download Tumblr Videos?

The Permanent URL is: How to Link Static Library in C/C++ using GCC compiler?

2 Comments

Leave a Reply