gcc和ld 中的参数 --whole-archive 和 --no-whole-archive

转自gcc和ld 中的参数 --whole-archive 和 --no-whole-archive


首先 --whole-archive --no-whole-archiveld专有的命令行参数,gcc 并不认识,要通gcc传递到 ld,需要在他们前面加-Wl,字串。

--whole-archive 可以把 在其后面出现的静态库包含的函数和变量输出到动态库,--no-whole-archive 则关掉这个特性。

比如你要把 liba.a  libb.a libc.a 输出到 libabc.dll(或libabc.so)时应该这么写:

libabc.dll:liba.c libb.a libc.a

       gcc  -shared -o $@ -L. -Wl,--whole-archive -la -lb -lc -Wl,--no-whole-archive

在--whole-archive作用下的库里不能有函数同名。

 

下面有一个包含Makefile 和c的完整例子:

#dllMake
all:ap lib
exe=main.exe
dll=libabc.dll

lib:$(dll)
ap:$(exe)

src=main
sa = a b c
$(exe):$(src:%=%.c) $(dll)
 gcc $< -labc -L. -o $@
$(dll):$(sa:%=lib%.a)
 gcc  -shared -o $@ -L. -Wl,--whole-archive $(sa:%=-l%) -Wl,--no-whole-archive
lib%.a:%.o
 ar rcs  $@ $<
%.o:%.c
 gcc $< -g -c -o $@ 
 
clean:
# -rm $(sa:%=%.o)
# -rm $(src:%=%.o) 
 -rm $(sa:%=lib%.a)
 -rm $(dll) 
 -rm $(exe)

//a.c
#include <stdio.h>
int func_a(int arg1)
{
 printf("a %d\n" , arg1);
 
 return 25*arg1;
}
int samefun() 
{
 printf("%s in %s\n", __FUNCTION__ , __FILE__ ) ;
}
 
// b.c
#include <stdio.h>
int func_b(int arg1)
{
 return 2*arg1;
}
 
// c.c
#include <stdio.h>
int func_c(int arg1)
{
 return 3*arg1;
}
 
//main.c
#include <stdio.h>
int main()
{
 int arg = 412; 
 int res ;
 printf("start\n");
 res =  func_a(arg)+func_b(arg)+func_c(arg);
 printf(" %d => %d \n" , arg , res );
  samefun();
  
 return res;
}



转载于:https://www.cnblogs.com/noble/p/4144163.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值