当前位置: 首页 > article >正文

软硬链接与动静态库概览

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 rows
            for (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;
}

http://www.kler.cn/news/302170.html

相关文章:

  • 【Python机器学习】循环神经网络(RNN)——循环网络的记忆功能
  • 如何在Chrome中使用HTML构建交互式网页
  • sklearn-逻辑回归-特征工程示例
  • 深度学习-02 Pytorch
  • 安卓显示驱动
  • Flutter 响应式框架
  • Ubuntu20如何设置网络
  • 监控系统添加vcenter上的esxi主机
  • Kafka高吞吐量的原因
  • 苹果的“AI茅”之路只走了一半
  • Unity3D 自定义Debug双击溯源问题详解
  • 何为信创?信创有哪些?
  • FPGA技术赋能云数据中心:提高性能与效率
  • DevOps -分布式追踪与监控
  • 自定义Spring-start学习笔记
  • php转职golang第二期
  • 9.13学习记录
  • 分布式本地缓存 ehcache 缓存同步复制
  • Javaweb项目实现文件导出功能
  • 服务器数据恢复—Linux操作系统环境下网站数据的恢复案例
  • Java、python、php、node.js版 铁路售票自动选座系统 高铁购票系统 火车订票平台(源码、调试、LW、开题、PPT)
  • Android Graphics 显示系统 - VirtualDisplay的初印象 - 简单示例
  • .Net 中各种线程同步锁
  • Gitea Action 简单配置(CI/CD)
  • java 学习从零到精通之历程
  • C4D2025来了!亮眼的新功能一览
  • 高亚科技与广东海悟携手,打造全流程电子竞标管理平台!
  • 《程序猿之设计模式实战 · 策略模式》
  • 深度解读MySQL意向锁的工作原理机制与应用场景
  • 使用Selenium与WebDriver实现跨浏览器自动化数据抓取