当前位置: 首页> 财经> 创投人物 > 软硬链接与动静态库概览

软硬链接与动静态库概览

时间:2025/7/30 4:01:35来源:https://blog.csdn.net/taduanlangan/article/details/142153851 浏览次数:0次
1. 硬链接

关键词 ln

  • ln file newname

Linux 系统中,通过 inode 找到磁盘文件;

file.txt 创建硬链接,相当于让新的文件名(字符串)对同一个 inode 建立映射关系,引用计数++

rm file.txt 使 引用计数-- ,当引用计数减为 0 时,才对磁盘文件进行删除。

  • 创建一个新的目录

要了解 “为什么一个新目录的引用计数为 2”,首先需要明晰一个概念:Linux 中,一切皆文件

意味着,目录也是一个文件,保存着该目录内文件的文件名与其 inode 的映射关系

cd file

. 代表当前文件file;

创建一个新文件,“file” 和 “.” 同时建立了与 inode — 528280 的映射关系,故引用计数为 2。

“blog” “blog/.” “blog/file/…” 都与 inode — 528284 建立了映射关系,故引用计数为 3,以此类推…

2. 软链接

软链接(符号链接) 相当于 Windows 中的快捷方式

ln -s file newfile

创建软链接时,[-s] 为必选项

硬链接 是通过 inode 引用另外一个文件,软链接 是通过 名字 引用另外一个文件;

软链接本身是一个独立的文件,且有自己的 inode 。

3. 动静态库

静态库是一组预先编译好的目标文件集合,它们在程序的链接阶段被合并到最终的可执行程序中;

动态库是在程序运行时,才被加载到内存中的库,不会在编译阶段合并到可执行程序中。

当动静态库同时存在时,OS 优先使用动态库

生成并使用动静态库 :
// matrix.h matrix.c test.c 在文末给出
  • 静态库

gcc -c matrix.c -o matrix.o // 将 [所有.c] 文件编译成 [.o] 文件

ar -rc libmymatrix.a -o matrix.o // … lib[库名].a -o [+所有必须的.o文件]

gcc test.c -L. -lmymatrix // -L[库所在路径] -l[库名]

  • 动态库

gcc -fPIC -c matric.c // gcc -fPIC -c [所有必须的 .c 文件]

gcc -shared -o libmymatrix.so matrix.o //

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:[库所在路径] // = 前后不能有 [空格] ;将 库所在路径 添加到 环境变量"LD_LIBRARY_PATH"

步骤三也可以通过 1. 将 libmymatrix.so 拷到系统库目录下 2. 将 libmymatirx.so 在系统库目录下创建软链接 等方式解决 —— OS 能查找到我们的库即可。

gcc test.c -L. -lmymatrix

// matrix.h
#pragma 1#include <stdbool.h>#undef NULL 
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif// 矩阵置零
void setZeroes(int** matrix, int matrixSize, int* matrixColSize);
// 螺旋矩阵
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize);
// 旋转图像
void rotate(int** matrix, int matrixSize, int* matrixColSize);
// 搜索二维矩阵
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target);
// matrix.c
#include "matrix.h"// 矩阵置零
void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {int m = matrixSize;int n = matrixColSize[0];int row[m], col[n];memset(row, 0, sizeof(row));memset(col, 0, sizeof(col));for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (!matrix[i][j]) {row[i] = col[j] = true;}}}for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (row[i] || col[j]) {matrix[i][j] = 0;}}}
}// 螺旋矩阵
int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {if (matrixSize == 0 || matrixColSize[0] == 0) {*returnSize = 0;return NULL;}int rows = matrixSize, columns = matrixColSize[0];int visited[rows][columns];memset(visited, 0, sizeof(visited));int total = rows * columns;int* order = malloc(sizeof(int) * total);*returnSize = total;int row = 0, column = 0;int directionIndex = 0;for (int i = 0; i < total; i++) {order[i] = matrix[row][column];visited[row][column] = true;int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {directionIndex = (directionIndex + 1) % 4;}row += directions[directionIndex][0];column += directions[directionIndex][1];}return order;
}// 旋转图像
void rotate(int** matrix, int matrixSize, int* matrixColSize) {for (int i = 0; i < matrixSize / 2; ++i) {for (int j = 0; j < (matrixSize + 1) / 2; ++j) {int temp = matrix[i][j];matrix[i][j] = matrix[matrixSize - j - 1][i];matrix[matrixSize - j - 1][i] = matrix[matrixSize - i - 1][matrixSize - j - 1];matrix[matrixSize - i - 1][matrixSize - j - 1] = matrix[j][matrixSize - i - 1];matrix[j][matrixSize - i - 1] = temp;}}
}// 搜索二维矩阵
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target) {int m = matrixSize, n = matrixColSize[0];int i = 0, j = n - 1; // 从右上角开始while (i < m && j >= 0) { // 还有剩余元素if (matrix[i][j] == target) {return true; // 找到 target}if (matrix[i][j] < target) {i++; // 这一行剩余元素全部小于 target,排除} else {j--; // 这一列剩余元素全部大于 target,排除}}return false;
}
// test.c
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"// 打印矩阵
void printMatrix(int** matrix, int size) {for (int i = 0; i < size; ++i) {for (int j = 0; j < size; ++j) {printf("%d ", matrix[i][j]);}printf("\n");}printf("\n");
}int main() {int matrixSize = 4;int* matrixColSize = &matrixSize;// 初始化矩阵int** matrix = malloc(matrixSize * sizeof(int*));if (!matrix) {fprintf(stderr, "Failed to allocate memory for matrix.\n");return 1;}for (int i = 0; i < matrixSize; ++i) {matrix[i] = malloc(matrixSize * sizeof(int));if (!matrix[i]) {fprintf(stderr, "Failed to allocate memory for row %d.\n", i);// Free already allocated rowsfor (int j = 0; j < i; ++j) {free(matrix[j]);}free(matrix);return 1;}}// 填充矩阵for (int i = 0; i < matrixSize; ++i) {for (int j = 0; j < matrixSize; ++j) {matrix[i][j] = (i + 1) * 10 + (j + 1);}}// 输出原始矩阵printf("Original Matrix:\n");printMatrix(matrix, matrixSize);// 旋转矩阵rotate(matrix, matrixSize, matrixColSize);// 输出旋转后的矩阵printf("Rotated Matrix:\n");printMatrix(matrix, matrixSize);// 释放内存for (int i = 0; i < matrixSize; ++i) {free(matrix[i]);}free(matrix);return 0;
}
关键字:软硬链接与动静态库概览

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: