1.静态库
准备源程序和头文件
头文件
#include <myhead.h>
int add(int m, int n)
{
return m+n;
}
#include <myhead.h>
int sub(int m,int n)
{
return m-n;
}
源程序
#ifndef _ADD_H_
#define _ADD_H_
int add(int m, int n);
#endif
#ifndef __SUB_H_
#define __SUB_H_
int sub(int m,int n);
#endif
将源程序编译成静态库
gcc -c add.c -o add.o //只编译不链接,生成一个源程序的二进制文件
gcc -c sub.c -o sub.o //只编译不链接,生成一个源程序的二进制文件
ar -crs libcaculate.a add.o sub.o //依赖于add.o和sub.o生成一个caculate.a的静态库
使用静态库
gcc main.c -L ./lib -lcaculate -I ./include/
gcc 主程序.c -L 库所在的路径 -l库名 -I 头文件所在的路径
-L:指定库的路径
-l:指定需要使用的库
-I:指定头文件所在的路径
2.动态库
准备对应的源程序和头文件
头文件
#include <myhead.h>
int add(int m, int n)
{
return m+n;
}
#include <myhead.h>
int sub(int m,int n)
{
return m-n;
}
源程序
#ifndef _ADD_H_
#define _ADD_H_
int add(int m, int n);
#endif
#ifndef __SUB_H_
#define __SUB_H_
int sub(int m,int n);
#endif
将源程序编译生成动态库
gcc -fPIC -shared add.c sub.c -o libcaculate.so
使用动态库
gcc main.c -L /lib -lcaculate -I ./include/
3.进程
创建进程
#include <myhead.h>
int main(int argc, const char *argv[])
{
sleep(1);
//产生一个进程
fork();
fork();
fork();
fork();
while(1);
return 0;
}