c程序如何生成并使用共享库

一、前言

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值