How to create and use static / dynamic libraries in C

Purpose

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.

Conclusion first

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!)

Sample code

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.

C language global variable declaration

Creation of dynamic library and operation check

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

Creating a static library and checking its operation

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

Check again when dynamic and static libraries coexist

#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

Summary

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

Each reference

Refer to the following contents for creating a dynamic library

17.3. Creating Dynamic Libraries with GCC

Refer to the following contents for creating a static library

[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

How to create and use static / dynamic libraries in C
How to use Google Test in C
How to use is and == in Python
How to use the C library in Python
How to generate permutations in Python and C ++
How to use Decorator in Django and how to make it
How to install and use Tesseract-OCR
How to use classes in Theano
How to use SQLite in Python
How to use pyenv and pyenv-virtualenv in your own way
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
How to use .bash_profile and .bashrc
Comparison of how to use higher-order functions in Python 2 and 3
How to install and use Graphviz
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to create dataframes and mess with elements in pandas
How to use functions in separate files Perl and Python versions
[C language] How to create, avoid, and make a zombie process
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
How to use Anaconda interpreter in PyCharm
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use Python zip and enumerate
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use pandas Timestamp and date_range
[Introduction to pytorch-lightning] How to use torchvision.transforms and how to freely create your own dataset ♬
How to use lists, tuples, dictionaries, and sets
Introducing Sinatra-style frameworks and how to use them
How to create explanatory variables and objective functions
How to use Python Image Library in python3 series
How to multi-process exclusive control in C language
Summary of how to use MNIST in Python
How to create data to put in CNN (Chainer)
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
How to create a Rest Api in Django
How to write async and await in Vue.js
How to install Cascade detector and how to use it
How to plot autocorrelation and partial autocorrelation in python
Tips for those who are wondering how to use is and == in Python
How to use "Jupyter Book" to create publication quality books and documents from .ipynb
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means