静态库、动态库回顾

回顾一下库相关的知识点,总结备忘一下。在某种情况下,你有了如下的代码,结构如下

c 复制代码
//pra.h
#include <stdio.h>
void test_01();
//pra.c
#include "pra.h"
void test_01() {
    printf("xxxxxxx----->%s %s()\n", "pra", __FUNCTION__);
}

//test.h
#include <stdio.h>
void test_01();
__attribute ((visibility("default"))) void test_02();
//test.c
#include "test.h"

void test_01() {
    printf("xxxxxxx----->%s()\n", __FUNCTION__);
}

__attribute ((visibility("default"))) void test_02() {
    printf("xxxxxxx----->%s()\n", __FUNCTION__);
}

//main.c
#include "pra/pra.h"
#include "test/test.h"

int main() {
    test_01();
    return 0;
}

假设,pra和test分别代表第三方动态库。

小白需要让编译上述代码并运行,编译过程如下

c 复制代码
//生成动态库libtest.so
gcc -fPIC -shared test.c -o libtest.so
//生成动态库libpra.so
gcc -fPIC -shared pra.c -o libpra.so

1、两个动态库含有同名函数

a. 不做特殊处理的时候

链接的时候,哪个库在前,用哪个库里面的函数实现

b. 通过fvisibility进行控制

假设,main函数只希望执行libtest.so的test_01函数

可以在编译libpra.so加入-fvisibility=hidden

bash 复制代码
gcc -fPIC -shared -fvisibility=hidden pra.c -o libpra.so

这样随意调整动态库顺序,最终只会调用libtest.so对应的函数

通过readelf查看一下,可以看到libpra.so中test_01函数是LOCAL.

2、两个动态库含有同名函数

bash 复制代码
ar -rv libtest.a test.o
ar -rv libpra.a pra.o

哪个静态库在前,用哪个静态库的函数

3、反编译

3.1 查看动态库源码

编译代码的时候需要加入-g参数

bash 复制代码
gcc -fPIC -shared -g pra.c -o libpra.so
objdump --section=.text -S libpra.so

可以得到如下的结果,最后一小节就是源码了

3.2 查看静态库源码

先生成静态库

bash 复制代码
gcc -g -c pra.c -o pra.o
ar -rv libpra.a pra.o

静态库的反编译需要先将.a文件还原成.o文件,再查看。

bash 复制代码
ar -x libpra.a //会得到pra.o
objdump --section=.text -S pra.o
相关推荐
青瓦梦滋几秒前
【在线五子棋对战】游戏匹配模块
网络·c++·网络协议·http·https
陌上花开缓缓归以9 分钟前
linux 内核4.4 内存分配使用分析
linux·运维·服务器
David猪大卫32 分钟前
【C++修炼】STL容器—模板进阶:非类型模板参数、模板特化、模板分离编译
c语言·开发语言·c++·经验分享·笔记·学习·面试
欢乐的小猪1 小时前
Linux命令 chmod
linux·运维·服务器
纪念 2291 小时前
C++ vector(一)
开发语言·c++
Happy_Sharing1 小时前
Windows虚拟机安装ubuntu后,如何在虚拟机Ubuntu系统中访问Windows文件夹
linux·运维·ubuntu
垆边人似月.1 小时前
最少的元素个数使和为 target
数据结构·c++·算法
码上有光1 小时前
Linux:信号的认识、信号的产生、信号的保存、信号的处理
android·linux·运维·信号
Zip-List1 小时前
工作小记-pytorch的算子优化: 转gpu核函数 python调c
c语言·c++·pytorch·python·cuda
暖焰核心2 小时前
多态全解——虚函数、重写、虚表与多态原理
c++