用命令行编译Qt程序

用命令行编译Qt程序

一、准备工作

创建 main.cpp 文件,放置在空文件夹中

#include <QMainWindow>
#include <QPushButton>
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMainWindow w;
    QPushButton b(&w);
    b.setText("Hello Qt!");

    w.show();

    return a.exec();
}

在这里插入图片描述

二、命令行操作

开始菜单中找到Qt的文件夹,打开Qt的命令行程序。这样不用配置 path
在这里插入图片描述
在这里插入图片描述

1、qmake -project,生成 .pro 工程文件

在这里插入图片描述

2、qmake,生成 Makefile 文件

在这里插入图片描述

3、mingw32-make 编译程序

在这里插入图片描述
出错了,原因来自 qmake -project 生成的工程文件不完整:

######################################################################
# Automatically generated by qmake (3.1) Thu Nov 18 10:38:20 2021
######################################################################

TEMPLATE = app
TARGET = demo
INCLUDEPATH += .

# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
SOURCES += main.cpp

加入相应的模块引用:

######################################################################
# Automatically generated by qmake (3.1) Thu Nov 18 10:38:20 2021
######################################################################
QT       += core
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TEMPLATE = app
TARGET = demo
INCLUDEPATH += .

# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
SOURCES += main.cpp

4、大功告成
在这里插入图片描述
在这里插入图片描述
说明:
1、gui 模块是旧版本,新版本中用 wingets 替代了。
2、理论上直接 QT += widgets 是没问题了,但我这边却报错。不知道什么原因,有知道的烦请留言告之。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值