MinGW编译dll并引用
记得某位神仙曾经说过:一个项目不使用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并引用的更多相关文章
- 利用openssl管理证书及SSL编程第3部分:将MinGW编译的openssl dll导出def和lib供MSVC使用
将MinGW编译的openssl dll导出def和lib供MSVC使用 前面我们用mingw把openssl 编译成了动态库,得到下面2个dll文件: libeay32.dll ssleay32.d ...
- 找不到编译动态表达式所需的一种或多种类型。是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?
提示“找不到编译动态表达式所需的一种或多种类型.是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用? ”错误 解决方法: 将引入的COM对象(misc ...
- MinGW g++.exe 编译 DLL 时,导出函数名带@的问题
今天尝试用CodeBlocks写了一个简单的Dll,发现生成的 dll 文件导出的函数名后面都有一个 @xxx 从生成的 libDll2.def 中看到: EXPORTS DllMain@ @ Max ...
- 手把手教你在Windows下使用MinGW编译libav(参考libx264的编入)
转自:http://www.th7.cn/Program/cp/201407/242762.shtml 手把手教你在Windows下使用MinGW编译libav libav是在Linux下使用纯c语言 ...
- 使用mingw编译完整Qt5的过程(使用了niXman的msys套装)good
使用mingw编译完整Qt5的过程 坛子里似乎已经有人编译出Qt5了,不过大多有问题,不是缺少opengl就是缺少openssl,还有缺少webkit的,本文提供的仍然不能说是绝对完整的,不过相对以前 ...
- Fedora 11中用MinGW编译Windows的Qt4程序(在Linux系统下编译Windows的程序)
Ubuntu下可以直接安装: sudo apt-get install mingw32 mingw32-binutils mingw32-runtime 安装后编译程序可以: i586-mingw32 ...
- MingW编译virt-viewer
在http://www.spice-space.org/download.html可以下载到windows版本的virt viewer virt-viewer-x86-0.5.7.msi和virt-v ...
- 在Windows环境下使用MinGW编译Qt 4.8.6
1.修改环境变量工具推荐:Rapid Environment Editor.官网:http://www.rapidee.com/ 修改前请先备份当前的环境变量.然后: (1)检查系统变量path,删除 ...
- 使用Mingw编译wxSqlite3-3.0.5
最近在学习wxWidgets,而且官方也出了3.0版本,貌似还不错的样子,准备做个小程序来练手.中间需要用到数据库看到很多人推荐wxSqlite3就去下来看看,以下是我使用TDM-GCC 4.8.1( ...
随机推荐
- a++ 和 ++a
//a++ 先赋值,后加 var a = 1 console.log(a++);//1 console.log(a) //2 //++a 先赋值,后加 var a = 1 console.log(++ ...
- BZOJ5137&&lg4081(广义后缀自动机,set启发式合并)
BZOJ5137&&lg4081(广义后缀自动机,set启发式合并) 题面 自己找去 HINT 给定多个文本串,让你查询每个文本串中有多少个本质不同的子串且这个子串只出现在当前这个文本 ...
- SYZOJP186 你猜猜是不是DP 二分+hash解法
SYZOJP186 你猜猜是不是DP题解 题目传送门 现在给两个仅包含小写字母的字符串a,b ,求a 与b的最长公共连续子串的长度. 对于20%的数据,a,b长度 ∈ [1, 200] 对于50%的数 ...
- vue项目常用方法封装,持续更新中。。。
vue项目中可以直接使用 1.常用工具类untils.js中 /* * 验证手机号是否合格 * true--说明合格 */ export function isPhone(phoneStr){ let ...
- 获取properties文件的内容
获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...
- react 事件传参数
class Demo extends React.Component { click = (a) => (e) => { console.log('额鹅鹅鹅', a, e); } rend ...
- 如何与GitHub同步,将本地文件push到到远程仓库
Run git config --global user.email "you@example.com" git config --global user.name "Y ...
- java8的ConcurrentHashMap为何放弃分段锁,为什么要使用CAS+Synchronized取代Segment+ReentrantLock
原文地址:https://cloud.tencent.com/developer/article/1509556 推荐一篇 ConcurrentHashMap 和 HashMap 写的比较的的文章 j ...
- Pytest学习7-参数化
在测试过程中,参数化是必不可少的功能,本文就讨论下pytest的几种参数化方法 @pytest.mark.parametrize:参数化测试函数 1.内置的pytest.mark.parametriz ...
- linux - mysql 异常:/usr/bin/which: no mysql in
问题描述 运行:which mysql 报错:/usr/bin/which: no mysql in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local ...