使用mingw制作dll文件
2024-10-01 20:28:15
使用mingw制作dll文件
准备math.c文件
//math.c
#include<stdio.h>
int add(int a,int b){
return a+b;
}
int sub(int a,int b){
return a-b;
}
int mul(int a,int b){
return a*b;
}
int div(int a,int b){
return a/b;
}
制作dll
gcc math.c -shared -o math.dll -Wl,--out-implib,math.lib
生成 math.dll、math.lib
验证dll
编写加载dll文件的代码
#include<stdio.h>
#include <windows.h>
typedef int (*AddFunc)(int ,int);
typedef int (*SubFunc)(int ,int);
typedef int (*MulFunc)(int ,int);
typedef int (*DivFunc)(int ,int);
int main(void)
{
int a=10,b=2;
HMODULE hDll = LoadLibrary("math.dll");
if (hDll != NULL)
{
AddFunc add = (AddFunc)GetProcAddress(hDll, "add");
SubFunc sub = (SubFunc)GetProcAddress(hDll, "sub");
MulFunc mul = (MulFunc)GetProcAddress(hDll, "mul");
DivFunc div = (DivFunc)GetProcAddress(hDll, "div"); if (add != NULL)
printf("a+b=%d\n",add(a,b));
if (sub != NULL)
printf("a-b=%d\n",sub(a,b));
if (mul != NULL)
printf("a*b=%d\n",mul(a,b));
if (div != NULL)
printf("a/b=%d\n",div(a,b)); FreeLibrary(hDll);
}
}
编译
gcc loaddll_dynamic_show.c -o loaddll_dynamic_show.exe
运行
loaddll_dynamic_show.exe
结果
a+b=12
a-b=8
a*b=20
a/b=5
最新文章
- 【原创】PageAdminCMS 前台SQL注入漏洞(1)
- Git Shell 基本命令(官网脱水版)
- 【转载】AngularJs 指令directive之controller,link,compile
- Android菜鸟成长记6 -- 网络连接的检查
- 使用Servlet实现图片下载
- android复习第二天------布局
- PCA样本数量少于矩阵维数
- GetQueuedCompletionStatus的返回值
- C++:String类
- 浅谈c语言程序为什么需要内存 栈又是什么?
- hdu1159 LCS模板题
- Android逆向工程
- QMainWindow
- JS关于Date函数的格式化输出
- Jquery Ajax Realize whether the user is registered
- Safari 3D transform变换z-index层级渲染异常的研究
- JavaScript 上万条数据 导出Excel文件 页面卡死
- win10 搭建react-native开发环境
- 【Atcoder】AGC027 题解
- CentOS下Redisserver安装配置