Static Linking and Static Libraries (also known as an archive) is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries are, .a files in Linux and .lib files in Windows.
Dynamic linking and Dynamic Libraries Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. If multiple programs in the system link to the same dynamic link library, they all reference the library. Therefore, this library is shared by multiple programs and is called a “shared library” . Examples of Dynamic libraries are, .so in Linux and .dll in Windows.
advantages
disadvantages
Static Library
1. Make the executable has fewer dependencies, has been packaged into the executable file. 2. The link is completed in the compilation stage, and the code is loaded quickly during execution.
1. Make the executable file larger. 2. Being a library dependent on another library will result in redundant copies because it must be packaged with the target file. 3. Upgrade is not convenient and easy. The entire executable needs to be replaced and recompiled.
Dynamic Library
1.Dynamic library can achieve resource sharing between processes, there can be only one library file. 2. The upgrade procedure is simple, do not need to recompile.
1. Loading during runtime will slow down the execution speed of code. 2. Add program dependencies that must be accompanied by an executable file.
We want to create a static(or dynamic) library by function.cpp and call the static library in main.cpp. This time we write two CMakeLists.txt files, one in CmakeDemo4 folder and another in lib folder.
1
# Search the source files in the current directory
2
# and store them in the variable SRC
3
aux_source_directory(. LIB_SRC)
4
5
# Create a static library named MyFunction
6
add_library(MyFunction STATIC ${LIB_SRC})
1
# CMake minimum version
2
cmake_minimum-required(VERSION 3.10)
3
4
# Project name
5
project(CMakeDemo4)
6
7
# Search the source files in the current directory
using “g++ -S -o” to generate assembly code based on x64. (The CPU of the testing machine is based on x64)
In x64, register “rsp” is stack pointer.
here passing the value of a huge struct needs more stack space (1040+8+1032) than passing the value of a pointer which points the buge struct(16+1040).
using “aarch64-linux-gnu-g++ -S -o” to generate assembly code based on ARM64.
here passing the value of the huge struct needs more stack space (16+64+2032) than passing the value of a pointer which points the buge struct(32+1056).
在给定的代码中,变量 e 是一个指向 int 的指针,它通过 malloc 函数分配了内存。malloc 函数用于动态分配堆内存,因此 e 指向的内存空间属于堆(heap)
1
intadd3_x(int&c, int&d) {
2
c++; // 增加 c 的值
3
d++; // 增加 d 的值
4
int sum = c + d; // 将 c 和 d 的新值加到 sum 上
5
return sum; // 返回 sum
6
}
3-2. Change fdemo3_ptr.c on page 23 to pass the reference instead of pass the pointer, generate the assembly soure code on your PC and answer the question: Would passing the reference use more stack space than passing the pointer in this situation ?