Windows下使用MakeFile(Mingw)文件

原因:本人想在windows下建立Qt工程並編譯,安裝qt-opensource-windows-x86-mingw530-5.7.0.exe包,使用集成開發環境IDE編譯無問題,可是本人想試一下make本地編譯生成xxx.exe結果遇到些問題,在此分享html

根據QT4.0書上說明構建步驟以下:(以helloQt項目名爲例)windows

  1.在命令提示符下,進入hello目錄,輸入以下命令,生成一個與平臺無關hello.pro:app

  qmake -projectide

  2.而後,鍵入以下命令,從這個項目生成一個與平臺相關的makefile文件:工具

  qmake helloQt.proui

  3.鍵入make命令就能夠構建改程序。spa

遇到問題:命令行

  1.首相windows下沒有qmake命令,可是安裝完qt-opensource-windows-x86-mingw530-5.7.0.exe包以後,安裝目錄下就有一個qmake.exe文件,可以使用everything工具全盤搜索,便可見到,debug

  如今咱們有兩種方案,1,在qmake.exe文件夾目錄下運行qmake指令(僅限此目錄下能夠識別qmake);2.添加環境變量到path下,就能夠任何目錄下運行qmake。code

  執行完qmake -project命令生成平臺無關文件helloQt.pro內容以下:

######################################################################
# Automatically generated by qmake (3.0) ?? ?? 16 19:35:06 2016
######################################################################

TEMPLATE = app
TARGET = helloQt
INCLUDEPATH += .

# Input
HEADERS += mainwindow.h ui_mainwindow.h
FORMS += mainwindow.ui
SOURCES += main.cpp mainwindow.cpp

  2.執行完qmake helloQt.pro命令生成平臺相關的makefile文件

#############################################################################
# Makefile for building: helloQt
# Generated by qmake (3.0) (Qt 5.7.0)
# Project:  helloQt.pro
# Template: app
# Command: D:\ProgramFilies\Qt5.7.0\5.7\mingw53_32\bin\qmake.exe -o Makefile helloQt.pro
#############################################################################

MAKEFILE      = Makefile

first: release
install: release-install
uninstall: release-uninstall
QMAKE         = D:\ProgramFilies\Qt5.7.0\5.7\mingw53_32\bin\qmake.exe
DEL_FILE      = del
CHK_DIR_EXISTS= if not exist
MKDIR         = mkdir
COPY          = copy /y
COPY_FILE     = copy /y
COPY_DIR      = xcopy /s /q /y /i
INSTALL_FILE  = copy /y
INSTALL_PROGRAM = copy /y
INSTALL_DIR   = xcopy /s /q /y /i
DEL_FILE      = del
SYMLINK       = $(QMAKE) -install ln -f -s
DEL_DIR       = rmdir
MOVE          = move
SUBTARGETS    =  \
        release \
        debug

......

  3.運行make找不到命令,發現安裝目錄下有一個mingw32-make.exe猜想此文件便是make文件,添加環境變量

  運行命令mingw32-make

  結果報錯,g++命令沒法識別,再繼續在安裝目錄下找到g++.exe添加環境變量,

  繼續運行mingw32-make,報錯mainwindow.h:4:23: fatal error: QMainWindow: No such file or directory

D:\QT\helloQt>mingw32-make
mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'D:/QT/helloQt'
g++ -c -pipe -fno-keep-inline-dllexport -O2 -std=gnu++11 -frtti -Wall -Wextra -f
exceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEE
DS_QMAIN -I. -I. -I..\..\ProgramFilies\Qt5.7.0\5.7\mingw53_32\include -I..\..\Pr
ogramFilies\Qt5.7.0\5.7\mingw53_32\include\QtGui -I..\..\ProgramFilies\Qt5.7.0\5
.7\mingw53_32\include\QtANGLE -I..\..\ProgramFilies\Qt5.7.0\5.7\mingw53_32\inclu
de\QtCore -Irelease -I..\..\ProgramFilies\Qt5.7.0\5.7\mingw53_32\mkspecs\win32-g
++  -o release\main.o main.cpp
In file included from main.cpp:1:0:
mainwindow.h:4:23: fatal error: QMainWindow: No such file or directory
compilation terminated.
Makefile.Release:118: recipe for target 'release/main.o' failed
mingw32-make[1]: *** [release/main.o] Error 1
mingw32-make[1]: Leaving directory 'D:/QT/helloQt'
Makefile:34: recipe for target 'release' failed
mingw32-make: *** [release] Error 2

  找不到QMainWindow頭文件,此時無從下手,此helloQt工程是用IDE建立的而且編譯沒問題,不過此時我再次使用IDE編譯發現報一樣的錯誤mainwindow.h:4:23: fatal error: QMainWindow: No such file or directory

  鬱悶之極,怎麼會找不到這個文件

  ...

  此時又從新建一個工程對比文件發現IDE生成的helloQt.pro文件有差異,IDE生成的helloQt.pro文件以下:

 

#-------------------------------------------------
#
# Project created by QtCreator 2016-10-15T18:10:09
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qt_empty
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

 

  比較發現命令行qmake -project生成的文件helloQt.pro少兩行

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets  

  添加完這兩行以後運行mingw32-make 編譯成功生成helloQt.exe

  完美!!!

  運行正常!!!

  

  後話:

  在仔細思考以後發現這兩段文字中的粗體部分,平臺無關平臺相關,

  1.在命令提示符下,進入hello目錄,輸入以下命令,生成一個與平臺無關hello.pro:

  qmake -project

  2.而後,鍵入以下命令,從這個項目生成一個與平臺相關的makefile文件:

  可能正是這兩句話致使我出錯,由於我是在windows下運行生成了平臺無關的hello.pro文件

  QT       += core gui

  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

   再查閱資料(.pro文件命令格式)發現

  QT:指定工程索要使用的QT模塊(默認的是core gui,對應於QtCore和QtGui模塊)

  查閱QT幫助文檔

  QMainWindow Class

The QMainWindow class provides a main application window. More...

Header:

#include <QMainWindow>

qmake:

QT += widgets

Inherits:

QWidget

  發現類QMainWindow繼承自QWidget,因此須要添加widgets模塊

  因此問題根因一步步揭曉:

  1.少添加core gui 模塊與widgets模塊 同時還能夠進行版本判斷。

 

  QT += core gui

 
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

  若有問題歡迎各路大神進行指點...不吝賜教
相關文章
相關標籤/搜索