Linux系统编程 27 静态库的使用已经头文件对应
学习笔记
gcc ***.c lib***.a -o file.out
添加-Wall,显示所有警告信息
$ls
libmymath.a resource test.c test.o test.out
$gcc test.c libmymath.a -o test.out -Wall
test.c: In function ‘main’:
test.c:7:5: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration]
printf("%d+%d=%dn",a,b,add(a+b));
^
test.c:8:5: warning: implicit declaration of function ‘sub’ [-Wimplicit-function-declaration]
printf("%d-%d=%dn",a,b,sub(a,b));
^
test.c:9:5: warning: implicit declaration of function ‘div1’ [-Wimplicit-function-declaration]
printf("%d/%d=%dn",a,b,div1(a,b));
^
test.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
}
警告出现在编译阶段,因为出现在编译阶段
隐式声明
函数调用时候
1.函数定义要在前面
2.声明在前面
如果上面两点都没有的时候,那么编码器会进行隐式声明
且只会一种隐式声明
1.返回值类型固定为int
2.参数根据参数类型 (存疑)
int add(int,int)
$vi test.c
$gcc test.c ./libmymath.a -o test.out
$vi mymath.h
$cat mymath.h
#ifndef _MYMATH_H_
#define _MYMATH_H_
extern int add(int a,int b);
extern int div1(int a,int b);
extern int sub(int a,int b);
#endif
$ls
libmymath.a mymath.h resource test.c test.out
注意
$$gcc ./libmymath.a test.c -o test.out
bash: ./libmymath.a: Permission denied
注意要源码写在前面,库写在后面。
将lib文件放入lib文件夹
将头文件放入inc头文件
$mkdir inc
$mkdir lib
$mv ./*.h ./inc
$mv libmymath.a ./lib
$ls
inc lib resource test.c test.out
$rm test.out
$ls
inc lib resource test.c
$gcc test.c ./lib/libmymath.a -I./inc -o test.out
$ls
inc lib resource test.c test.out
$./test.out
4+6=10
4-6=-2
4/6=0
$



