gcc动态库fPIC和fpic编译选项差异介绍
作者以前并没有特别留意fPIC和fpic之间的差别,最近在aarch64服务器上编译遇到的一个问题,让自己好好的查询了一些资料,或许平常的开发人员并不容易遇到它,但愿对有缘的读者有一定帮助。
1)出动态库时,合并libnettle.a库遇到下面错误提示
/data/compilerdir/bin/opensdk/linux64/lib/libnettle.a(fat-arm64.o): in function `fat_init':
/data2/compilerdir/nettle/fat-arm64.c:198:(.text.startup+0x184): relocation truncated to fit: R_AARCH64_LD64_GOTPAGE_LO15 against symbol `_nettle_aes192_decrypt_c' defined in .text section in /data/compilerdir/bin/opensdk/linux64/lib/libnettle.a(aes192-decrypt.o)
/usr/bin/ld: /data/compilerdir/nettle/fat-arm64.c:198: warning: too many GOT entries for -fpic, please recompile with -fPIC
collect2: error: ld returned 1 exit status
/data2/compilerdir/bin/libhyscoder.so] Error 1
其中关键提示为
warning: too many GOT entries for -fpic, please recompile with -fPIC
经查证 fpic 和 fPIC 比较
相同点:都是为了在动态库中生成位置无关的代码。通过全局偏移表(GOT)访问所有常量地址。程序启动时动态加载程序解析GOT条目。
不同点:如果链接的可执行文件的GOT大小超过计算机架构特定的最大值,则会在编译链接时报错误消息,提示 -fpic 不起作用;因此在这种情况下,需要使用 -fPIC 重新编译。GOT大小因芯片架构的不同而大小不一样,SPARC上为8k,在AArch64上为28k(笔者遇到的就是它,GOT超出了限制,所以报错),在m68k和RS / 6000上为32k。而x86上没有此限制。
提示
为了保障程序在跨平台编译时整体可用,通常情况下建议都用fPIC
参考来源地址
https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
英文原文(如果上面地址不能访问,可以参考下面原文)
-fpic
Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.When this flag is set, the macros __pic__ and __PIC__ are defined to 1.-fPIC
If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on AArch64, m68k, PowerPC and SPARC.Position-independent code requires special support, and therefore works only on certain machines.When this flag is set, the macros __pic__ and __PIC__ are defined to 2.
gcc动态库fPIC和fpic编译选项差异介绍相关推荐
- C语言 gcc 动态库
动态库编译例子 代码准备 |-- fn | |-- add.c | `-- sub.c |-- h | `-- head.h |-- lib `-- test.c add.c内容 int add(in ...
- gcc 动态库的编译和使用
1.制作出来的库,如何给用户使用? 头文件 *.h 制作出来的库 *.so 2.动态库的命名规则(三部分) lib + 自定义库的名字 + .so 3.制作动态库的步骤 1 将.c文件生成.o[语法] ...
- Linux中gcc的编译、静态库和动态库的制作
欢迎大家关注笔者,你的关注是我持续更博的最大动力 Linux中gcc的编译.静态库.动态库 文章目录: 1 gcc的编译过程 1.1 gcc的编译过程 1.2 gcc的常用参数 2 gcc 静态库的制 ...
- Linux下用gcc生成静态库和动态库及练习使用OpenCV
我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为 静态库和动态库两种.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而 ...
- gcc生成静态库.a和动态库.so和OpenCV3.4.11的安装及使用示例
gcc生成静态库.a和动态库.so和OpenCV3.4.11的安装及使用示例 文章目录 gcc生成静态库.a和动态库.so和OpenCV3.4.11的安装及使用示例 一.用gcc生成静态库和动态库 ( ...
- C++静态库与动态库
C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...
- c++静态库和动态库
C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...
- C++静态库与动态库的区别?
C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...
- 静态库与动态库之间的区别
什么是库 库是写好的,现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说,库是一种可执行代码的二进制形式,可以 ...
最新文章
- vue中把props中的值赋值给data
- 验证身份证和中文名字
- swift:创建表格UITableView
- MFC中CString转换成char数组的问题
- 偷梁换柱 | 无备份情况下的数据恢复实践
- MySQL数据库操作步骤---增删改查
- 第 0 章 阳哥MySQL高级
- TCP/IP协议损坏解决方法:
- 高并发、高性能 Web 架构
- listview mysql源码_用ListView实现对数据库的内容显示
- 大数据表转移hdfs后查询处理
- web视频播放器videojs
- “项目集管理标准体系与PgMP认证”讲座笔记
- Ubuntu16.04安装视频播放器
- 发现了JMX的好东西,大家分享一下!!
- 如何在渗透测试中尽可能隐藏自己-proxychains4使用配置教程
- 解决WIN 7 不能任意修改无线网卡MAC地址的问题
- python学习笔记一——鸭子模型
- 2023年副业赚钱最快的方法,最后一个时代红利,你一定要抓住!
- android 如何判断去电或来电已经接通