C/C++工程函数重命名

面对C/C++项目中可能出现的符号冲突问题,尤其是当使用多个第三方库时,手动解决函数重命名效率低下。通过学习和实践,利用clang的分词功能结合sed命令,可以有效提高重命名的效率,避免大规模的手动修改。文章介绍了符号冲突的原因、解决思路,并提供了相关学习资源和实际操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用clang接口实现工程函数重命名

问题背景

在c/c++大项目中我们会使用到其他部门提供的so动态库,如果两个部门函数命名不规范出现符号冲突的几率非常大。历史原因我们需要在几十万代码中对函数重命名,手工来改肯定是无法接受的,在解决问题过程中发现,clang的分词配合sed命令来修改很好提高了效率。

符号冲突

学习地址: https://www.cnblogs.com/skynet/p/3372855.html
书籍: 程序员的自我修养——链接、装载与库
解决:https://blog.csdn.net/found/article/details/105263450

  1. 通一个源文件中出现两个相同符号出现编译错误如:
  #include <stdio.h>
  #include <stdlib.h>
  static void test(int a)
  {
   
      printf("file:%s func:%s %d\n",__FILE__, __FUNCTION__, a);
  }
  
  void test(int a)
  {
   
      printf("file:%s func:%s %d\n",__FILE__, __FUNCTION__, a);
  }
  
  #include <stdio.h>
  #include <stdlib.h>
  static void test(int a)
  {
   
      printf("file:%s func:%s %d\n",__FILE__, __FUNCTION__, a);
  }
  
  static void test(int a)
  {
   
      printf("file:%s func:%s %d\n",__FILE__, __FUNCTION__, a);
  }
  1. 内部static 符号和外部提供so导出冲突,内部使用时使用static函数
  2. 内部导出符号和外部符号冲突,调用时使用内部函数
  3. 程序连接的so导出符号冲突
    1). 程序运行时会加载so动态库对外提供的函数名到全局符号表。如果发现so中函数已经被加载到全局符号表中,后面的函数会被忽略。
 //test1.c  gcc -shared -fPIC -o libtest1.so test1.c
  #include <stdio.h>
  #include <stdlib.h>
  void test(int a)
  {
   
      printf("file:%s func:%s %d\n",__FILE__, __FUNCTION__, a);
  }

  //test2.c gcc -shared -fPIC -o libtest2.so test2.c
  #include <stdio.h>
  #include <stdlib.h>
  void test(int a)
  {
   
      printf("file:%s func:%s %d\n",__FILE__, __FUNCTION__, a);
  }
  
  //main.c  gcc main.c -o main  -L./ -ltest1 -ltest2 -Xlinker --rpath ./
  //输出
  #include <stdio.h>
  #include <stdlib.h>
  void test(int a);
  void test_a();
  void test_b();
  void main()
  {
   
      test(1);
  }

2). 连接库加载是按照广度优先加载的 比如 下面依赖会先加载 liba.so libb.so libc.so 最后加载libd.so
main -->libtest1.so
—> libtest2.so—>lib.so
—> libtest3.so

//gcc -shared -fPIC -o lib.so test3.c
  #include <stdio.h>
  #include <stdlib.h>
  void test()
  {
   
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值