zoukankan      html  css  js  c++  java
  • 使用mingw制作dll文件

    使用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
        ",add(a,b));
                     if (sub != NULL)
                        printf("a-b=%d
        ",sub(a,b));
                     if (mul != NULL)
                        printf("a*b=%d
        ",mul(a,b));
                     if (div != NULL)
                        printf("a/b=%d
        ",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
        
  • 相关阅读:
    python 获取时间戳
    【转载】Git分支
    【转载】Jmeter分布式测试
    【总结】异常处理
    【转载】linux-查看日志
    【转载】python中if-else的多种写法
    【转载】Linux中rz和sz命令
    【转载】pip 使用国内源
    wrk(一)
    angular-gridster2使用
  • 原文地址:https://www.cnblogs.com/tonghaolang/p/9253995.html
Copyright © 2011-2022 走看看