示例文件
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