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( ...
随机推荐
- 变量 Variables
是为了存储(store)程序(program)运算过程中的一些信息(informations),为了方便日后调用.操作和更改 变量名应该简明,见名识意,让读者和我们自己能更清晰的了解 如果我们把变量看 ...
- windows中Read函数引发数据异常的问题
[摘要] 在Window C/S开发中少不了客户端与服务端数据通信的情况,每当客户端从服务端获取数据时会将数据读到本地本件或缓存中,例如通过CInternetFile类的Read函数会将网卡缓存中的数 ...
- MySQL的排序(order by)
MySQL的排序(order by) 1.降序(DESC) 2.升序(ASC) 1. 降序(DESC) 完整代码: SELECT `学号`,`考试日期`,`考试成绩` FROM `表2`ORDER B ...
- Android_侧滑菜单的实现
1.创建侧滑菜单Fragment package com.example.didida_corder; import android.os.Bundle; import android.view.La ...
- numpy学习(二)
练习篇(Part 2) 11. Create a 3x3 identity matrix (★☆☆) arr = np.eye(3) print(arr) 运行结果:[[1. 0. 0.] [0. 1 ...
- [CF1034B] Longest Palindrome - 贪心
如果自己是回文串可以做中心 如果一个串和另一个串的转置相等则可以凑一对 优先配对 #include <bits/stdc++.h> using namespace std; int n,m ...
- BK: Data mining
data ------> knowledge Are all patterns interesting? No. only a small fraction of the patterns po ...
- Vim入门——Windows下安装
下载页面:https://www.vim.org/download.php Windows选用的是MS-Windows: 下图为展示: 因为最近被墙,镜像貌似没中国内陆地区,因此,选择使用GitHub ...
- python 删除git Jenkinsfile文件
背景:在做ci集成的发现分支超过100个之后,pipline activity列表中前期的分支会被隐藏,这导致master分支在活动视图中不可见 解决方案:删除历史分支的Jenkinsfile 分支太 ...
- JDBC——Connection数据库连接对象
功能 1.获取执行SQL的对象 方法:createStatement() 用于创建向数据库发送SQL语句的一个对象.修饰/返回值类型:Statement(声明) 方法:prepareStatement ...