c/c++基础(十七) 编译与加载 动态库(.so)

本文介绍了如何在C/C++中创建和加载动态库(.so)。首先,展示了将`filea.c`和`fileb.c`编译为`first.so`库,并将该库链接到`testso`程序的步骤。接着,由于动态库的查找路径问题,运行`testso`时遇到错误,通过设置`LD_LIBRARY_PATH`环境变量解决了问题。最后,演示了如何在代码中使用`dlopen`和`dlfcn`函数动态加载`first.so`库,并成功运行`CLoadSo`程序。
摘要由CSDN通过智能技术生成

c文件filea.c:

#include <stdio.h>
void fun1()
{
	printf("i am from filea fun1 \n");
}

c文件fileb.c:

#include <stdio.h>
void fun2()
{
	printf("i am from fileb fun2 \n");
}

测试文件testso.c

void fun1();
void fun2();
int main(int argc,char *argv[])
{
	fun1();
	fun2();
	return 0;
}


1.将filea.c fileb.c编译成so库供testso.c调用,命令如下:


gcc -Wall -fpic -shared filea.c fileb.c -o first.so




2.编译testso.c并链接共享库first.so

gcc -Wall testso.c first.so -o testso


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值