创建静态库的基本步骤

开始创建的目录情况如下:

创建的几个文件分别是:

    1,pmath.h #ifndef P_MATH_H //头文件卫士 #define P_MATH_H //函数的声明 int p_add (int x, int y); int p_sub (int x, int y); int p_mul (int x, int y); int p_div (int x, int y); #endif 2,p_add.c #include "pmath.h" int p_add (int x, int y) { return x + y; } int p_sub (int x, int y) { return x - y; } 3,p_mul.c #include "pmath.h" int p_mul (int x, int y) { return x * y; } int p_div (int x, int y) { return x / y; } 4,test.c #include "Imath/pmath.h" #include <stdio.h> int main (void) { int a = 6, b = 2; printf (" %d + %d = %d ", a, b, p_add (a, b)); printf (" %d - %d = %d ", a, b, p_sub (a, b)); printf (" %d * %d = %d ", a, b, p_mul (a, b)); printf (" %d / %d = %d ", a, b, p_div (a, b)); return 0; }

我这里是在同一目录下创建的这四个文件,所以我需要将 test.c 移动到上级目录。( 当然你也可以直接在上级目录创建 test.c :)

将要加入静态库的文件编译为目标文件。链接 .c 文件生成 .o :

将目标文件添加到静态库(creating libImath):

查看:

使用静态库链接生成可执行文件并执行:

目录结构 :

经验分享 程序员 微信小程序 职场和发展