【Cpp】C语言使用静态库

博客介绍了静态库创建及编译执行的步骤,包括创建文件目录,编写hello.h、hello.c和main.c文件,创建静态库,最后进行编译和执行操作。

1,文件目录

root@rose:~/test# tree
.
|-- hello.c
|-- hello.h
|-- hello.o
|-- libadd.a
|-- main
`-- main.c

2,创建hello.h和hello.c
2.1. hello.h

#ifndef __HELLO__
#define __HELLO__

int add(int a, int b);

#endif

2.2 hello.c

int add(int a, int b)
{
  return a+b;
}

3,创建静态库

ar -rcs libadd.a hello.o # 生成静态库,libadd.a

4,创建main.c

#include<stdio.h>
#include<math.h>
#include "hello.h"   # 调用静态库,引入头文件

int main()
{
  double y = sin(2.0);
  int sum = add(3, 5);
  printf("the result = %f\n",y);

  printf("the sum = %d\n", sum);

  return 0;
}

5,编译

gcc -o main main.c  -L ./ -ladd    # -L 静态库存放路径,-ladd,具体的静态库名字

6,执行

root@rose:~/test# ./main
the result = 0.909297
the sum = 8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值