一、前言
在开发大型应用或者是集成第三方功能(比如集成算法)的时候,通常都是直接调用共享库中的接口的。使用共享库,不仅能够避免程序体量过大,还便于多个程序使用公共的功能。
本文将介绍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);
#endif
2.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)