Construction and use of dynamic library and static library

1. Understand dynamic library and static library

Static, dynamic refers to the link

The so-called static and dynamic refers to links . To review, the steps of compiling a program into an executable program:
Linking : Add the code of the external function (usually files with the suffixes .lib and .a) to the executable file. This is called linking. This way of adding an external function library to an executable file by copying is called static linking (static linking), and dynamic linking (dynamic linking)

naming method

Under linux:
Static library: lib library name.a
Dynamic library: lib library name.so(shared object)

Window: Next
Static library: lib library name.lib
Dynamic library: lib library name.dll(dynamic link library)

Distinguish a concept: a library is a whole that includes the prefix lib and the suffix .a or .so

The library name is the remaining part after removing the prefix and suffix


Characteristics of dynamic library and static library

The characteristics of the static library:
1. When using the static library, the information of the static library will be directly compiled into the executable file.
2. Advantages: When the static library is deleted, it has no effect on the executable file.
3. Disadvantage: Waste of memory space. If the static library is modified, the executable program needs to be recompiled

The characteristics of the dynamic library:
when the loader loads the dynamic library, the operating system will first check whether the dynamic library has been loaded into the memory by other programs. If it is not loaded into memory, the operating system will load the dynamic library into memory and set its reference count to 1; if it has been loaded into memory, it will only add 1 to the reference count of the dynamic library.

2. Build dynamic and static libraries

Project directory

Haha.cpp just write something, such as naming a function, print "haha"

CMakeList.txt

aux_source_directory(./ SOURCES)

add_executable(example ${SOURCES})

# 添加动态库 得到libhaha.so动态库文件
add_library(haha SHARED haha.cpp)
# 给动态库添加版本号 得到libhaha.so.1.2  同时得到两个软链接 具体看结果说明
set_target_properties(haha PROPERTIES VERSION 1.2 SOVERSION 1)

# 添加静态库  得到libhaha.a静态库文件
add_library(haha_static STATIC haha.cpp)
# 更改输出库的名字 libhah.a 修改为 libhello.a 
set_target_properties(haha_static PROPERTIES OUTPUT_NAME "hello")

# 设置可执行文件的输出路径 这是是工程目录下的lib目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# 设置库文件的输出路径 这是是工程目录下的lib目录
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

Result description

After running, you will get a lib directory, which contains executable files and the dynamic library and static library files we built.

Open the lib directory:

The libhello.a file is a static library file, which can be obtained by the following command:

# 添加静态库  得到libhaha.a静态库文件
add_library(haha_static STATIC haha.cpp)
# 更改输出库的名字 libhah.a 修改为 libhello.a
set_target_properties(haha_static PROPERTIES OUTPUT_NAME "hello")

The libhaha.so.1.2 file is a static library file, and libhaha.so.1 soft link libhaha.so soft link, obtained by this command:

# 添加动态库 得到libhaha.so动态库文件
add_library(haha SHARED haha.cpp)
# 给动态库添加版本号 得到libhaha.so.1.2  同时有两个链接
#  VERSION指代动态库版本, SOVERSION 指代API版本
set_target_properties(haha PROPERTIES VERSION 1.2 SOVERSION 1)

3. Use dynamic libraries

Conditions for using dynamic libraries

There must be a header file of the dynamic library, here is the haha.h header file

You must know the path where the dynamic library is located:

CMakeLists.txt

# 动态库所在目录
set(lib_dir "/home/jason/work/my-deploy/example/lib")

# 添加 动态库头文件 搜索路径
include_directories(./)

# 生成可执行文件
add_executable(example main.cpp)

# 链接库文件到可执行文件  库文件名称要写全
target_link_libraries(example ${lib_dir}/libhaha.so)

In mai.cpp, the header files of the dynamic library need to be applied before the functions of the dynamic library can be used

Here the header file is haha.h, and the dynamic library function is haha(), which will print haha!

#include <iostream>
#include "haha.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    haha();
    return 0;
}

4. Use a static library

Using a static library is basically the same as using a dynamic library. You also need to prepare the path where the static library is located, and the header file of the static library

CMakeLists.txt

When linking the library file, just change it to the static library name

# 静态库所在目录
set(lib_dir "/home/jason/work/my-deploy/example/lib")

# 添加 静态库头文件 搜索路径
include_directories(./)

# 生成可执行文件
add_executable(example main.cpp)

# 链接库文件到可执行文件  库文件名称要写全
target_link_libraries(example ${lib_dir}/libhello.a)

reference:

[Cmake Actual Combat: Fanwai] Library, dynamic library and static library (.dll, .so, .lib, .a)_cmake dll and lib_ Zheng's Notes Blog-CSDN Blog

[cmake combat 6] How to use the compiled library (dynamic library dll)——windows system_cmake compile dynamic library_ Zheng's Notes Blog-CSDN Blog [cmake combat
5] How to use the compiled library (static library)—— windows system_windows compile static library_Zheng classmate's notes blog-CSDN blog

 Do I have to have a header file to create a static library/dynamic library? Why? _Is a header file required to use a static library?_Eating watermelon dipped in hot sauce blog-CSDN blog
What is the difference between using a static library and directly using the source code in c++? _The difference between generating static library links and direct compilation_Qianmo Yangyang's Blog-CSDN Blog

C++ Compilation (3)-camke/CMakeLists.txt Compilation and Use Tutorial - Programmer Sought

Guess you like

Origin blog.csdn.net/weixin_45824067/article/details/130803643