Gcc链接选项

关于gcc链接选项可以通过页面https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-z。一般通过-Wl,option来传递参数给链接器。


  • -soname

-soname用于指定动态链接库名字,用法: -Wl,-soname,libxxx.so。

  • --gc-sections

--gc-sections表示依赖库中不使用的sections将不被链接,可以减小链接目标的大小,一般跟-ffunction-sections一起使用。用法:-Wl,--gc-sections。

  • -shared

生成动态库。(Produce a shared object which can then be linked with other objects to form an executable. )用法:

gcc -shared foo.o -o libfoo.so
  • --whole-archive

--whole-archive用来告诉链接器,将后面库中出现的所有符号都链接进来,这样有一个好处是链接的时候不需要考虑中间文件和库文件的顺序,以保证链接能够成功。

  • --no-whole-archive

--no-whole-archive用于重置设置的 --whole-archive。

  • -z,noexecstack

标记当前链接的目标文件是不需要executable stack的。

  • -z,execstack

标记当前链接的目标文件需要executable stack。

  • -z,relro

relro:Relocation Read Only。Create an ELF "PT_GNU_RELRO" segment header in the object。也就是会创建一个PT_GNU_RELRO段,这个重定向区域时只读的。这个是用来保护目标文件防止被篡改,编译器一般都会默认带上此参数。

  • -z,now

-z,now常用于跟-z,relro一起使用,用于保护生成的目标文件被篡改。ld的手册中是这样介绍的:When generating an executable or shared library, mark it to tell the dynamic linker to resolve all symbols when the program is started, or when the shared library is linked to using dlopen, instead of deferring function call resolution to the point when the function is first called.设置这个参数表明在程序运行或者dlopen这个库的时候就将所有的动态符号加载并绑定进来,而不是在动态符号需要使用的时候加载。

  • --build-id

--build-id用于创建elf文件中".note.gnu.build-id"段信息。这个段中的信息是链接时的唯一标识,一般使用md5或者哈希计算(md5或者sha1),如果不使用可以传入"none"。

  • --fatal-warnings

--fatal-warnings将所有警告当错误处理,相当于-Werror,可以通过--no-fatal-warnings取消。

  • --no-undefined

链接的时候如果依赖库中包含未定义符号,那么没有设置--no-undefined选项的时候,链接是不会报错的,但是运行时如果用到了未定义符号则运行时报错。加入了-Wl,--no-undefined,编译链接时会报错。

  • -rpath

-rpath指定运行时搜索路径。用法:-Wl,-rpath,/data/data/testlib/lib,这样在运行的时候,linker会在/data/data/testlib/lib下查找依赖库。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值