c程序生成并使用共享庫的操作方法
一、前言
在開發(fā)大型應(yīng)用或者是集成第三方功能(比如集成算法)的時候,通常都是直接調(diào)用共享庫中的接口的。使用共享庫,不僅能夠避免程序體量過大,還便于多個程序使用公共的功能。
本文將介紹c程序如何生成共享庫以及其他程序如何使用該共享庫。
二、如何生成共享庫
我們來寫一個實(shí)現(xiàn)加減乘功能的共享庫。
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 將源文件編譯成目標(biāo)文件
使用gcc編譯器將程序源碼編譯成目標(biāo)文件,并添加-fPIC以生成與位置無關(guān)的代碼,這是生成共享庫所必須的,如下所示:
(2.2-1)
2.3 將目標(biāo)文件鏈接成共享庫
使用gcc將目標(biāo)文件鏈接成共享庫。共享庫的命名通常遵循特定的約定,例如在Linux系統(tǒng)上,共享庫通常以 lib
開頭,并以 .so
結(jié)尾
如下圖所示:
(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)
運(yùn)行結(jié)果如下:
(3.2-2)
到此這篇關(guān)于c程序生成并使用共享庫的操作方法的文章就介紹到這了,更多相關(guān)c程序共享庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(127.詞語階梯)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(127.詞語階梯),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實(shí)現(xiàn)LeetCode(34.在有序數(shù)組中查找元素的第一個和最后一個位置)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(34.在有序數(shù)組中查找元素的第一個和最后一個位置),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07