일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- openssl
- 앱스토어
- apple
- 안드로이드
- SWIFT
- otpkey
- albumbook
- OTP
- appres
- SwiftUI
- 2FA
- 인증
- SSH
- fido
- kmip
- 애플
- FIDO2
- MYSQL
- 앨범북
- git
- WebAuthn
- Android
- MSYS2
- Xcode
- SSL
- 앱리소스
- Nodejs
- OSX
- css
- MFA
- Today
- Total
인디노트
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.
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; } |
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; } |
Now, we can compile these into *.o object files.
1 | 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 |
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 |
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 |
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; } |
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 |
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 |
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)); |
However, the compiler still generates the binary code and you can run it:
1 2 3 | $ ./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); |
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.
'소스 팁 > C, C++, C#' 카테고리의 다른 글
Linux so 파일에 Shared 된 함수명 리스트 (0) | 2018.11.04 |
---|---|
[LINUX] gcc undefined reference to 오류 처리 하기 (0) | 2018.11.04 |
우분투에서 json-c library 설치 및 사용 (0) | 2018.11.01 |
[LINUX C] C언어에서 JSON (JSON-C) 데이터 읽기 (0) | 2018.10.22 |
멀티캐스트 송수신 (0) | 2018.06.29 |