Linux 使用gcc命令生成静态库和动态库
1、生成静态库
1、创建目录保存本次文件
2、编辑生成例子程序 hello.h、hello.c 和 main.c
程序1:hello.h
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H
程序2:hello.c
#include <stdio.h>
void hello(const char *name)
{
printf("Hello %s!\n", name);
}
程序3:main.c
#include "hello.h"
int main()
{
hello("everyone");
return 0;
}
3、将 hello.c 编译成.o 文件
在系统提示符下键入以下命令得到 hello.o 文件。
gcc -c hello.c
在 ls 命令结果中,看到 hello.o 文件
4、由.o 文件创建静态库
静态库文件名的命名规范是以 lib 为前缀,紧接着跟静态库名,扩展名为.a
在系统提示符下键入以下命令将创建静态库文件 libmyhello.a。
ar -crv libmyhello.a hello.o
在ls 命令结果中有 libmyhello.a
5、在程序中使用静态库
在系统提示符下键入以下命令生成目标程序hello
gcc main.c libmyhello.a -o hello