栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux系统编程 27 静态库的使用已经头文件对应

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux系统编程 27 静态库的使用已经头文件对应


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
$

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号