You can create two types of libraries on Linux, .a (static) and .so (dynamic), but they are used in a similar way when compiling. That is, even if libA.a and libA.so of different libraries exist, the extension of the library is omitted in the compile command and it becomes like gcc xxx -lA, so it is ambiguous which one is used. ..
I want to check the operation when libA.a and libA.so coexist, so make a note.
gcc -o main main.c -L. -lA When the library libA (specified by -lA) is specified, libA.so (dynamic) is linked in preference to libA.a (static).
*** Addendum *** gcc -o main main.c -L. -static -lA If you specify "-static" like this, libA.a (static) will be linked.
*** Addendum 2 *** gcc -o main main.c -L. -static-libgcc -Wl,-Bdynamic,-lc,-Bstatic,-lA It is possible to select a library by specifying "-Bdynamic" (dynamic) or "-Bstatic" (static) with the "-Wl" option of gcc. (Thank you very much @yumetodo!)
Use the code in the article below. You can also set num to 5 in the static library and num to 10 in the dynamic library to find out which one was used at compile time.
A.c
#include<stdio.h>
#include"A.h"
void A(){
num = 10; //Set num to 10 in dynamic library
printf("inside A, num = %d\n", num);
}
#Create a shared (dynamic) library for A
[pirlo@centos8 02]$ gcc -shared -o libA.so -c A.c
・ ・ ・
#Compile the main process and create an executable file
[pirlo@centos8 02]$ gcc -o main main.c -L. -lA -lB
#File list
[pirlo@centos8 02]$ ls
A.c A.h B.c B.h libA.so libB.so main main.c
Execution result
[pirlo@centos8 02]$ ./main
inside A, num = 10
inside B, num = 10
A.c
#include<stdio.h>
#include"A.h"
void A(){
num = 5; //Only here in the static library is different
printf("inside A, num = %d\n", num);
}
#Create a static library for A
[pirlo@centos8 02]$ gcc -c A.c B.c #object".o "file is created
[pirlo@centos8 02]$ ls
A.c A.h A.o B.c B.h B.o main.c
[pirlo@centos8 02]$ ar rcs libA.a A.o #Create a static library from an object file
[pirlo@centos8 02]$ ls
A.c A.h A.o B.c B.h B.o libA.a main.c
・ ・ ・ Abbreviation of B related processing ・ ・ ・
#Compile the main process and create an executable file (commands are exactly the same as when dynamic)
[pirlo@centos8 02]$ gcc -o main main.c -L. -lA -lB
#File list
[pirlo@centos8 02]$ ls
A.c A.h A.o B.c B.h B.o libA.a libB.a main main.c
Execution result
[pirlo@centos8 02]$ ./main
inside A, num = 5
inside B, num = 5
#File list
[pirlo@centos8 02]$ ls
A.c A.h A.o B.c B.h B.o libA.a libA.so libB.a libB.so main.c
#Recreate the executable with the same compile command
[pirlo@centos8 02]$ gcc -o main main.c -L. -lA -lB
Execution result
[pirlo@centos8 02]$ ./main
inside A, num = 10
inside B, num = 10
When dynamic and static libraries coexist, the dynamic library is preferentially referenced.
*** Addendum *** If you add the "-static" option, the static library will be referenced.
Execution result
[pirlo@centos8 02]$ ls
A.c A.h A.o B.c B.h B.o libA.a libA.so libB.a libB.so main.c
#「-No "static" option
[pirlo@centos8 02]$ gcc -o main main.c -L. -lA -lB
[pirlo@centos8 02]$ ./main
inside A, num = 10
inside B, num = 10
#「-With "static" option
[pirlo@centos8 02]$ gcc -o main main.c -L. -static -lA -lB
[pirlo@centos8 02]$ ./main
inside A, num = 5
inside B, num = 5
*** Addendum 2 *** You can select a library by specifying "-Bdynamic" or "-Bstatic" with the "-Wl" option of gcc.
Execution result
#Use A and B static libraries
[pirlo@centos8 02]$ gcc -o main main.c -L. -static-libgcc -Wl,-Bdynamic,-lc,-Bstatic,-lA,-lB
[pirlo@centos8 02]$ ./main
inside A, num = 5
inside B, num = 5
#Use all dynamic libraries
[pirlo@centos8 02]$ gcc -o main main.c -L. -static-libgcc -Wl,-Bdynamic,-lc,-lA -lB
[pirlo@centos8 02]$ ./main
inside A, num = 10
inside B, num = 10
[17.4. Creating static libraries with GCC and ar](https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/developer_guide/creating-libraries-gcc_creating-static-libraries -gcc)
Recommended Posts