使用mingw制作dll文件

  • 安装mingw

  • 准备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

最新文章

  1. 【原创】PageAdminCMS 前台SQL注入漏洞(1)
  2. Git Shell 基本命令(官网脱水版)
  3. 【转载】AngularJs 指令directive之controller,link,compile
  4. Android菜鸟成长记6 -- 网络连接的检查
  5. 使用Servlet实现图片下载
  6. android复习第二天------布局
  7. PCA样本数量少于矩阵维数
  8. GetQueuedCompletionStatus的返回值
  9. C++:String类
  10. 浅谈c语言程序为什么需要内存 栈又是什么?
  11. hdu1159 LCS模板题
  12. Android逆向工程
  13. QMainWindow
  14. JS关于Date函数的格式化输出
  15. Jquery Ajax Realize whether the user is registered
  16. Safari 3D transform变换z-index层级渲染异常的研究
  17. JavaScript 上万条数据 导出Excel文件 页面卡死
  18. win10 搭建react-native开发环境
  19. 【Atcoder】AGC027 题解
  20. CentOS下Redisserver安装配置

热门文章

  1. C++11 volatile 类型
  2. 使用bitmap处理海量数据
  3. Pku3664
  4. [Java开发之路](9)对象序列化与反序列化
  5. 具有可视化的功能的一款开源软件Gource
  6. java使double保留两位小数的多方法
  7. 使用Java语言开发微信公众平台(三)
  8. Excel 打开两个单独的页面
  9. Flume的可靠性
  10. Spring源码分析专题 —— 阅读指引