linux下静态库和动态库生成

示例文件

math.h:

#ifndef _MATH_H_
#define _MATH_H_

#include <stdio.h>

int add(int, int);

#endif

math.c:

#include <math.h>

int add(int x, int y) {
	return x + y;
}

编译静态库

命令

# 编译程.o文件,-c包含预处理、编译、汇编
gcc -c math.c

# 生成静态文件,并将math.o加入; -c创建库文件,-r插入.o到库文件
ar -rc libmath.a math.o
编译动态库

命令

gcc math.c -shared -fPIC -o libmath.so
ar命令简介
[root@localhost src]# ar --help
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV] [--plugin <name>] [member-name] [count] archive-file file...
       ar -M [<mri-script]
 commands:
  d            - delete file(s) from the archive #从库文件中删除文件
  m[ab]        - move file(s) in the archive #在库文件中移动文件,即调整文件次序
  p            - print file(s) found in the archive #打印库文件中所有文件内容
  q[f]         - quick append file(s) to the archive #快速将文件附加到库文件中
  r[ab][f][u]  - replace existing or insert new file(s) into the archive #替换已有文件或增加新文件到库文件中
  s            - act as ranlib 
  t            - display contents of archive #显示库文件包含哪些文件
  x[o]         - extract file(s) from the archive #从库文件中提取文件
 command specific modifiers:
  [a]          - put file(s) after [member-name] #将文件插入到指定文件后边
  [b]          - put file(s) before [member-name] (same as [i]) #将文件插入到指定文件前,与-i类似
  [D]          - use zero for timestamps and uids/gids
  [U]          - use actual timestamps and uids/gids (default)
  [N]          - use instance [count] of name
  [f]          - truncate inserted file names #截断插入文件的名字
  [P]          - use full path names when matching
  [o]          - preserve original dates
  [u]          - only replace files that are newer than current archive contents
 generic modifiers:
  [c]          - do not warn if the library had to be created
  [s]          - create an archive index (cf. ranlib)
  [S]          - do not build a symbol table
  [T]          - make a thin archive
  [v]          - be verbose
  [V]          - display the version number
  @<file>      - read options from <file>
  --target=BFDNAME - specify the target object format as BFDNAME
 optional:
  --plugin <p> - load the specified plugin
 emulation options: 
  No emulation specific options
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值