C language--static library and dynamic library

To be added: GitHub upload code.

Verification conclusion

There will be no conflict between windows and Linux containing functions with the same name in different dynamic libraries.

Preliminary knowledge

Static libraries and dynamic libraries are essentially programs. There are generally two purposes for using a library in a project. One is to make the program more concise and does not need to maintain too many source files in the project. The other is to keep the source code confidential. After all, not everyone wants to write their own The program is open sourced.

When we get the library files (dynamic library, static library), if we want to use them, we must have the declarations of the API functions provided in these libraries, that is, header files. Add these to the project, and we can write code happily .

static library

(1) Copy the relevant code in the static library to the executable file when compiling, and do not need to link the library when running

(2) There is no need to load the library when the program is running, and the running speed is fast (advantages)

(3) It takes up more disk and memory space. After the static library is upgraded, the program needs to be recompiled, and the library upgrade is inconvenient (disadvantage).
Suffix :均以.lib开头。Windows下静态库以.lib 结尾。Linux下以.a结尾。

Windows static library creation method:

When using VS to create, you can directly select a static library or a dynamic library when selecting an empty project, or you can modify it after creation.

Linux static library creation method:

生成静态库,需要先对源文件进行汇编操作 (使用参数 -c) 得到二进制格式的目标文件 (.o 格式), 然后在通过 ar 工具将目标文件打包就可以得到静态库文件了 (libxxx.a)。

When using the ar tool to create a static library, three parameters are required:

参数c:创建一个库,不管库是否存在,都将创建。
参数s:创建目标文件索引,这在创建较大的库时能加快时间。
参数r:在库中插入模块 (替换)。默认新的成员添加在库的结尾处,如果模块名已经在库中存在,则替换同名的模块。

step one:

Type the following command at the system prompt to get the hello.o file.

gcc -c hello.c

The naming convention of the static library file name under Linux is prefixed with lib, followed by the static library name, and the extension is .a. For example: the name of the static library we will create is hello, and the file name of the static library is libhello.a.

Step two:

Now that we have the .o file, let's package it. Create a static library with the ar command. Typing the following command at the system prompt will create the static library file libhello.a.

ar -rcs libmyhello.a hello.o //可接多个.o文件

3. Release the generated static library libmyhello.a and the corresponding header file head.h to the user.

Use a static library:

Connect, compile and run the static library, and specify the path and name of the static library when compiling.

-L: 指定库所在的目录 (相对或者绝对路径)
-l: 指定库的名字,需要掐头 (lib) 去尾 (.a) 剩下的才是需要的静态库的名字
gcc -o hello main.c -L. -l myhello
./hello

-o means to specify the name of the output file. If not specified, the default name will be generated according to the .c file. Here, hello is specified.
-main.c indicates the program file to use the static library.
-L specifies the third-party library directory, where -L. means to find the third-party library in the current directory.
-l myhello (pinch the head and remove the tail, with or without spaces between -l) means to connect the library named libhello.a or libhello.so.

dynamic library

(1) Only record which symbol (function) in which shared library (dynamic library) is used at compile time, do not copy the relevant code in the shared library, and load the shared library at runtime.
(2) The program does not contain the code in the library, and the code size is small (advantage).
(3) The library is easy to upgrade without recompilation (advantage).
(4) Use a wider
suffix : 均以.lib开头。在win系统下后缀为.dll,在Linux下后缀为.so。The library files obtained are different because of the different production tools used in windows.Note: Dynamic libraries have execution permissions, but static libraries do not.

Windows dynamic library creation method: When using VS to create, you can directly select a static library or a dynamic library when selecting an empty project, or you can modify it after creation.

Linux dynamic library creation method**:

生成动态链接库是直接使用 gcc 命令并且需要添加 -fPIC(-fpic) 以及 -shared 参数。

-fPIC 或 -fpic 参数的作用是使得 gcc 生成的代码是与位置无关的,也就是使用相对位置。
-shared参数的作用是告诉编译器生成一个动态链接库(无位置要求)。

step one:

Both static and dynamic libraries are created by .o files. Therefore, the .c file of the static library must be compiled into a .o file through gcc first, and the hello.o file is obtained by typing the following command at the system prompt.

gcc -c hello.c -fpic

Step two:

The naming convention of the dynamic library file name under Linux is prefixed with lib, followed by the static library name, and the extension is .so. For example: the name of the static library we will create is hello, and the file name of the dynamic library is libhello.so.

Create a dynamic library with the -shared parameter. Type the following command at the system prompt to create the dynamic library file libhello.so.

gcc -shared -o libhello.so hello.o,

The above two steps can be chained together:

gcc -shared -fPIC -o libhello.so hello.c

Use a dynamic library:

Generate an executable program.

gcc -o main main.c -L . -lhello
./main

When executing a program that uses a dynamic library, it may prompt that the dynamic library cannot be loaded

Linux提供了一个动态链接器:
The dynamic linker is a process independent of the application program and belongs to the operating system. When the user's program needs to load the dynamic library, the dynamic linker starts to work. Obviously, the dynamic linker does not know when the user compiles the program through gcc The path specified by the parameter -L.

So how does the dynamic linker search for a certain dynamic library? There is a default search order inside it, which is in order of priority from high to low:

1.可执行文件内部的 DT_RPATH 段
2.系统的环境变量 LD_LIBRARY_PATH //相当于操作系统提供的全局变量,里面可以存储一些路径。相当于PATH
3.系统动态库的缓存文件 /etc/ld.so.cache
4.存储动态库 / 静态库的系统目录 /lib/, /usr/lib 等
按照以上四个顺序,依次搜索,找到之后结束遍历,最终还是没找到,动态连接器就会提示动态库找不到的错误信息。

Solution (corresponding to 2, 3, 4 above):

We only need to put the path of the dynamic library into the corresponding 环境变量or 系统配置文件, and we can also put the dynamic library 拷贝到系统库目录(or put the soft link file of the dynamic library into these system library directories).

Solution 1: Add the library path to the environment variable LD_LIBRARY_PATH

View environment variable values:

echo $LD_LIBRARY_PATH // 此处$是为了取到LD_LIBRARY_PATH的值,不加的话会打印出个LD_LIBRARY_PATH

insert image description here

Solution 2: Update /etc/ld.so.cache file

insert image description here

Solution 3: Copy the dynamic library file to the system library directory /lib/ or /usr/lib (or put the soft link file of the library into it)

insert image description here
It is recommended to use soft links, so that even if the content in the dynamic library is modified, it does not need to be copied again.

Before starting the executable program, or after setting the dynamic library path, we can pass a 命令检测程序能不能够通过动态链接器加载到对应的动态库command called ldd:

ldd main(可执行的程序名称)

example

Verify whether two different dynamic libraries contain functions with the same name.

step one:

Create two dynamic libraries (select the dynamic library option when creating a new project or modify it in the project properties, please refer to the video tutorial: B station tutorial ): DLL_1, DLL_2, the two dynamic libraries contain the function get_info() with the same name. The content takes the dynamic link library DLL_1 as an example, and the corresponding modification function in the dynamic link library 2 prints the content as "This is the dynamic library 2...": The
content in dll1.h is:

#pragma once
#include <stdio.h>

#ifdef WIN32
#define DllExport    __declspec( dllexport )
#else
#define DllExport    __attribute__((visibility("default")))
#endif

DllExport
void
get_info();

The content in dll1.c is:

#include "dll1.h"

DllExport
void
get_info()
{
	printf("这是动态库1:dll_1\n");
}

Step two:

Now that there are two dynamic libraries, the next step is to write the main project DllTest to call the contents of the two dynamic libraries.
Notice:in. DLL_11 and DLL_22 are two static libraries, see the complete code and test resultsgithub:
The content in DllTest.h is:
insert image description here
The content in DllTest.c is:
TEST4:
insert image description here
TEST3:
insert image description here
TEST2:
insert image description here
TEST1:
insert image description here

Guess you like

Origin blog.csdn.net/qq_39030233/article/details/128606718