C++学习笔记

这篇博客介绍了如何使用QT的build工具和windeployqt命令来打包exe,确保所有依赖项都被包含。同时,讲解了在CMakeLists.txt中设置WIN32标志以避免控制台显示。此外,文章详细展示了在MinGW环境下,如何创建和调用DLL,包括编写头文件、源文件,编译成DLL以及测试DLL的步骤。
摘要由CSDN通过智能技术生成

QT 打包

找到生成的exe,根据build工具从开始菜单选择:
在这里插入图片描述
cd到exe目录,执行:

windeployqt 目标.exe

QT Cmake 无控制台

修改CMakeLists.txt加上WIN32:

  add_executable(untitled
    WIN32
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )

另外QMake的*.pro文件默认是这样的:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp

我们只需要把”CONFIG += console“注释掉即可。

MinGW gcc 调用dll

//mydll.h 
#ifndef _MY_DLL_H
#	define _MY_DLL_H_ 
#ifdef __cplusplus
extern "C" {
#endif 
extern __declspec (dllexport) int add_range (int, int);
extern __declspec (dllexport) double average (int*, int); 
#ifdef __cplusplus
}
#endif 
#endif // ~_MY_DLL_H_
//mydll.c
#include "mydll.h" 
// __declspec (dllexport) 
int add_range (int _sta, int _end) {	
	int res = 0;
	if (_sta > _end)
		return 0x7fffffff;
	for (; _sta != _end; _sta ++) {
		res += _sta;
	}
	return res;	
} 
// __declspec (dllexport)
double average (int* _arr, int _cnt) {
	double res = 0.0;
	int i = 0;
	if (_cnt <= 0)
		return (double) (~0);
	for (; i < _cnt; i ++)
		res += _arr [i];
	return (double) (res /= _cnt);
}
//先编译成目标文件:
gcc -m32 -c mydll.c
//再连接成dll:
gcc -m32 -shared -o mydll.dll mydll.o
//test.c 
#include <stdio.h>
#include "mydll.h" 
int main (int argc, char* argv []) {	
	int res = add_range (10, 200);
	printf ("%d\n", res);
	return 0;
}
//调用dll编译
gcc -m32  -o test.exe   test.c  mydll.dll
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值