目录
一、用gcc生成静态库和动态库
1.以hello world为例
(1)创建hello.c,hello.h,main.c程序
先在终端中输入代码
mkdir hello
cd hello
创建hello文件夹
然后使用gedit生成hello.c,hello.h,main.c三个文件,三个文件源代码如下
hello.c
#include <stdio.h>
void hello(const char *name)
{
printf("Hello %s\n", name);
}
hello.h
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H
main.c
#include "hello.h"
int main()
{
hello("world");
return 0;
}
(2)再生成.o文件
利用gcc -c hello.c生成hello.o的文件、
(3)生成.a静态库文件
静态库文件名的命名规范是以 lib 为前缀,紧接着跟静态库名,扩展名为.a。例如:我们将 创建的静态库名为 myhello,则静态库文件名就是 libmyhello.a。在创建和使用静态库时, 需要注意这点。创建静态库用 ar 命令。在系统提示符下键入以下命令将创建静态库文件 libmyhello.a。
ar -crv libmyhello.a hello.o
(4)在程序中使用静态库
输入代码
gcc main.c libmyhello.a -o hello
再输入./hello输出程序结果
</