一、前言
在开发大型应用或者是集成第三方功能(比如集成算法)的时候,通常都是直接调用共享库中的接口的。使用共享库,不仅能够避免程序体量过大,还便于多个程序使用公共的功能。
本文将介绍c程序如何生成共享库以及其他程序如何使用该共享库。
二、如何生成共享库
我们来写一个实现加减乘功能的共享库。
2.1 编写源代码
math_func.c代码如下:
/*************************************************************************
* auther: conbiao
* time: 2024/09/10
************************************************************************/
/************************************************************************
* header
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "math_func.h"
/************************************************************************
*function name: math_add()
*************************************************************************
* summary:
* caculate the sum of a and b
* parameters:
* a
* b
* return:
* the sum of a & b
*
************************************************************************/
int math_add(int a, int b)
{
return a + b;
}
/************************************************************************
*function name: math_sub()
*************************************************************************
* summary:
* caculate the sub of a and b
* parameters:
* a
* b
* return:
* the sub of a & b
*
***********************************************************************/
int math_sub(int a, int b)
{
return a - b;
}
/************************************************************************
*function name: math_multi()
*************************************************************************
* summary:
* caculate the product of a and b
* parameters:
* a
* b
* return:
* the product of a & b
*
***********************************************************************/
int math_multi(int a, int b)
{
return a * b;
}头文件math_func.h:
/*************************************************************************
> file name: math_func.h
> author: conbiao
> created time: 2024年09月10日 星期二 10时43分06秒
************************************************************************/
#ifndef math_func_h
#define math_func_h
/***********************************************************************
* header
***********************************************************************/
#include <stdio.h>
/***********************************************************************
* function declaration
***********************************************************************/
int math_add(int a, int b);
int math_sub(int a, int b);
int math_multi(int a, int b);
#endif2.2 将源文件编译成目标文件
使用gcc编译器将程序源码编译成目标文件,并添加-fpic以生成与位置无关的代码,这是生成共享库所必须的,如下所示:

(2.2-1)
2.3 将目标文件链接成共享库
使用gcc将目标文件链接成共享库。共享库的命名通常遵循特定的约定,例如在linux系统上,共享库通常以 lib 开头,并以 .so 结尾
如下图所示:

(2.3-1)
如此共享库就生成完成了。
三、使用共享库
在其他程序中如果要使用共享库,方法如下:
3.1 编写源码
-/*************************************************************************
> file name: lib_test.c
> author: conbiao
> created time: 2024年09月10日 星期二 11时13分11秒
************************************************************************/
#include <stdio.h>
#include "math_func.h"
/*************************************************************************
* main
************************************************************************/
int main(int argc, char* argv[])
{
int a = 10;
int b = 5;
printf("a + b = %d\n",math_add(a,b));
printf("a - b = %d\n",math_sub(a,b));
printf("a * b = %d\n",math_multi(a,b));
return 0;
}3.2 编译链接
编译并链接程序时,指定共享库的位置,如下所示:

(3.2-1)
运行结果如下:

(3.2-2)
到此这篇关于c程序生成并使用共享库的操作方法的文章就介绍到这了,更多相关c程序共享库内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论