记得某位神仙曾经说过:一个项目不使用dll简直是一场灾难。(滑稽)



这篇文章以A+B/A-B为范例,来介绍如何在MinGW下编译dll并引用。

  • 首先你要安装MinGW,并配置好环境变量(不配置环境变量也行,就是麻烦)。

  • 我们新建一段.cpp代码,命名为dll.cpp( 什么破名字 ),写入:

#include <iostream>
using namespace std; #define EXPORT __declspec(dllexport) extern "C"{
int EXPORT a_b(int a,int b); //计算A+B
int EXPORT a__b(int a,int b); //计算A-B
} int a_b(int a,int b){
return a+b;
} int a__b(int a,int b){
return a-b;
}

- 接下来建立一个名为test.cpp的代码文件( ~~又是这种破名字~~ ),里面写入:

#include <iostream>
#include <cstdio>
using namespace std; #define EXPORT __declspec(dllimport) extern "C"
{
int EXPORT a_b(int a,int b);
int EXPORT a__b(int a,int b);
} int main(){
cout << a_b(10,100*10) << endl;
cout << a__b(10*99, 100) << endl;
cin.get();
return 0;
}

现在来编译,打开cmd,输入命令 `g++ dll.cpp -shared -o dll.dll -Wl,--out-implib,dll.lib` 来把刚刚的 `dll.cpp` 编译成.dll。
接着输入 `g++ -ldll test.cpp -o test.exe` 来把 test.cpp 编译为 test.exe ,并且引用刚刚的 dll.dll。
怎么样?不出意外的话,你的目录下就会多出个test.exe,我们双击运行他。
输出的结果:
1010
890

  • 题外话:同时引用两个dll



    我们在刚才的目录下新建一段代码,名为dllx.cpp(呃……),里面写上:
#include <iostream>
using namespace std; #define EXPORT __declspec(dllexport) extern "C"{
int EXPORT a_x_b(int a,int b);
} int a_x_b(int a,int b){
return a*b;
}

并把test.cpp改成如下代码:

#include <iostream>
#include <cstdio>
using namespace std; #define EXPORT __declspec(dllimport) extern "C"
{
int EXPORT a_b(int a,int b);
int EXPORT a__b(int a,int b);
int EXPORT a_x_b(int a,int b);
} int main(){
cout << a_b(10,100*10) << endl;
cout << a__b(10*99, 100) << endl;
cout << a_x_b(10*10, 10) << endl;
cin.get();
return 0;
}

然后编译,输入命令 g++ dllx.cpp -shared -o dllx.dll -Wl,--out-implib,dllx.lib 来把刚刚的 dllx.cpp 编译成.dll。

接着再把test.exe编译一遍,输入命令 g++ -ldll -ldllx test.cpp -o test.exe 来编译test.exe。

怎么样,运行这个exe,是不是输出了10 * 10 * 10的计算结果?

MinGW编译dll并引用的更多相关文章

  1. 利用openssl管理证书及SSL编程第3部分:将MinGW编译的openssl dll导出def和lib供MSVC使用

    将MinGW编译的openssl dll导出def和lib供MSVC使用 前面我们用mingw把openssl 编译成了动态库,得到下面2个dll文件: libeay32.dll ssleay32.d ...

  2. 找不到编译动态表达式所需的一种或多种类型。是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?

    提示“找不到编译动态表达式所需的一种或多种类型.是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用? ”错误 解决方法:   将引入的COM对象(misc ...

  3. MinGW g++.exe 编译 DLL 时,导出函数名带@的问题

    今天尝试用CodeBlocks写了一个简单的Dll,发现生成的 dll 文件导出的函数名后面都有一个 @xxx 从生成的 libDll2.def 中看到: EXPORTS DllMain@ @ Max ...

  4. 手把手教你在Windows下使用MinGW编译libav(参考libx264的编入)

    转自:http://www.th7.cn/Program/cp/201407/242762.shtml 手把手教你在Windows下使用MinGW编译libav libav是在Linux下使用纯c语言 ...

  5. 使用mingw编译完整Qt5的过程(使用了niXman的msys套装)good

    使用mingw编译完整Qt5的过程 坛子里似乎已经有人编译出Qt5了,不过大多有问题,不是缺少opengl就是缺少openssl,还有缺少webkit的,本文提供的仍然不能说是绝对完整的,不过相对以前 ...

  6. Fedora 11中用MinGW编译Windows的Qt4程序(在Linux系统下编译Windows的程序)

    Ubuntu下可以直接安装: sudo apt-get install mingw32 mingw32-binutils mingw32-runtime 安装后编译程序可以: i586-mingw32 ...

  7. MingW编译virt-viewer

    在http://www.spice-space.org/download.html可以下载到windows版本的virt viewer virt-viewer-x86-0.5.7.msi和virt-v ...

  8. 在Windows环境下使用MinGW编译Qt 4.8.6

    1.修改环境变量工具推荐:Rapid Environment Editor.官网:http://www.rapidee.com/ 修改前请先备份当前的环境变量.然后: (1)检查系统变量path,删除 ...

  9. 使用Mingw编译wxSqlite3-3.0.5

    最近在学习wxWidgets,而且官方也出了3.0版本,貌似还不错的样子,准备做个小程序来练手.中间需要用到数据库看到很多人推荐wxSqlite3就去下来看看,以下是我使用TDM-GCC 4.8.1( ...

随机推荐

  1. 回味Ubuntu10.10致敬Gnome桌面

    目录 Ubuntu10.10可用源 Ubuntu10.10更新语言包 输入法支持 浏览器选择 文件下载 压缩文件中文乱码的处理 视频播放 科学计算 搭建Lamp环境 实现文件分享 主题美化 Ubunt ...

  2. C# WPF计算器界面(Calculator Design With Animations)

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  3. layedit不可编辑,按钮不能使用

    $("#LAY_layedit_1").contents().find("body[contenteditable]").prop("contente ...

  4. C# 修改/新建判断

    //查询有无重复(新建用) public List<bloodBreedDetailsEntity> CodeList(string code) { var expression = Ex ...

  5. Vue.js 源码构建(三)

    Vue.js 源码是基于 Rollup 构建的,它的构建相关配置都在 scripts 目录下. 构建脚本 通常一个基于 NPM 托管的项目都会有一个 package.json 文件,它是对项目的描述文 ...

  6. JUC-LOCK接口

    Synchronized 1.多线程编程模版上 (1)线程 操作 资源类 (2)高内聚低耦合 2.实现步骤 (1)创建资源类 (2)资源类里创建同步方法,同步代码块 3.例子:卖票 LOCK 接口 锁 ...

  7. SD卡报错“error -110 whilst initialising SD card”

    目前开发遇到了某些SD卡和TI的SOC芯片的驱动不协调的地方,具体表现为: uboot 阶段初始化mmc dev 1 没有任何串口信息输出,无法读写mmc Kernel阶段报错”SD卡初始化失败 er ...

  8. poj-2253(最小瓶颈路问题)

    题目链接 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...

  9. 错误:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

    pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', ...

  10. zabbix4.2配置监控MySQL

    1.在被监控主机安装好MySQL 相关步骤省略. 2.创建监控所需要的MySQL账户(MySQL服务器端) MariaDB [(none)]>grant usage on *.* to zabb ...