qmake的使用(可設置c編譯器flag參數)


本文由烏合之眾 lym瞎編,歡迎轉載 my.oschina.net/oloroso
***
還是先說一下當前的系統環境:Ubuntu 14.04 + Qt5.4
如果沒有安裝過QT,可以安裝下面幾個qt軟件

sudo apt-get install qt5-default qt5-doc-html qt5-qmake qt5-doc qt5-image-formats-plugins

這只是對qmake使用的一個說明而已。如果一直使用Qt Create來構建工程,很容易讓人以為Qt項目必須使用Qt Create來創建。其實我們可以像寫普通的C++工程一樣,不一定需要IDE,編輯器+編譯器即可搞定了。
不過這個有一個缺點,就是如果在connect函數連接信號的槽的時候,即便是槽函數不存在,也能夠通過編譯

1. 先來寫源代碼

必須先寫源代碼,這個源代碼很簡單,就是創建一個Widget,然后widget上面有一個PushButton,點擊之后彈出一個MessageBox來提示一個"hello world"

hello.h

#ifndef __HELLO_H_ #define __HELLO_H_ #include <QWidget> class hello:public QWidget{ Q_OBJECT public: explicit hello(QWidget *parent = 0); ~hello(); public slots: //槽函數,處理button單擊 void btn_click(); private: }; #endif #endif

hello.cpp

#include "hello.h" #include <QPushButton> #include <QMessageBox> hello::hello(QWidget *parent) : QWidget(parent){ //創建一個PushButton QPushButton * btn = new QPushButton("點擊我^-^",this); //連接信號和槽 connect(btn,SIGNAL(clicked()),this,SLOT(btn_click())); } void hello::btn_click() { QMessageBox::information(NULL, "單擊了button", "hello world", QMessageBox::Yes); }

main.cpp

#include "hello.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); hello w; w.show(); return a.exec(); }

2.編寫.pro文件

***.pro是Qt的工程文件,這個文件是給qmake用來生成Makefile用的。
如果了解makefile的人應該知道,Makefile的三個關鍵點就是目標依賴命令。這里也很類似。
.pro文件中可以指明這個Qt項目的頭文件源文件鏈接的外部庫目標文件名模板(生成什么樣的Makefile)版本配置(debug/release)等。
這里並不打算詳細介紹每一個部分,只做簡單介紹

.pro中變量 含義 示例
TEMPLATE 模板變量指定生成makefile(app:應用程序/lib:庫) TEMPLATE = app
QT 使用到的Qt定義的類(core/gui/widgets...) QT += widgtes
DESTDIR 指定生成的應用程序放置的目錄 DESTDIR += ../bin
TARGET 指定生成的應用程序名 TARGET = hello
HEADERS 工程中包含的頭文件 HEADERS += hello.h
FORMS 工程中包含的.ui設計文件 FORMS += hello.ui
SOURCES 工程中包含的源文件 SOURCES += main.cpp hello.cpp
RESOURCES 工程中包含的資源文件 RESOURCES += qrc/hello.qrc
LIBS 引入的lib文件的路徑 -L:引入路徑 LIBS += -L.
CONFIG 用來告訴qmake關於應用程序的配置信息 CONFIG+= qt warn_on release
UI_DIR 指定.ui文件轉化成ui_*.h文件的存放目錄 UI_DIR += forms
RCC_DIR 指定將.qrc文件轉換成qrc_*.h文件的存放目錄 RCC_DIR += ../tmp
MOC_DIR 指定將含Q_OBJECT的頭文件轉換成標准.h文件的存放目錄 MOC_DIR += ../tmp
OBJECTS_DIR 指定目標文件(obj)的存放目錄 OBJECTS_DIR += ../tmp
DEPENDPATH 程序編譯時依賴的相關路徑 DEPENDPATH += . forms include qrc sources
INCLUDEPATH 頭文件包含路徑 INCLUDEPATH += .
DEFINES 增加預處理器宏(gcc的-D選項)。 DEFINES += USE_MY_STUFF
QMAKE_CFLAGS 設置c編譯器flag參數 QMAKE_CFLAGS += -g
QMAKE_CXXFLAGS 設置c++編譯器flag參數 QMAKE_CXXFLAGS += -g
QMAKE_LFLAGS 設置鏈接器flag參數 QMAKE_LFLAGS += -rdynamic

hello.pro

因為我們這里比較簡單,就只寫需要的部分了。

# 使用到的Qt庫 QT += core widgets #目標文件名 TARGET = hello #生成應用程序 TEMPLATE = app #用到的cpp源文件 SOURCES += main.cpp hello.cpp #用到的cpp頭文件 HEADERS += hello.h

3. 使用qmake生成Makefile文件

qmake是Trolltech公司創建的用來為不同的平台和編譯器書寫Makefile的工具。是qt工具包的一部分.使用qmake作為Qt庫和Qt所提供的工具的主要連編工具。

qmake程序在Qt的安裝目錄下,比如我的機器上就在
o@o-pc:~/program_files/Qt5.4.1/5.4/gcc_64/bin$
你可以把這個路徑加入到系統環境變量的PATH變量中,這樣就可以隨便使用了。我不喜歡加入到環境變量,所以這里指定路徑來運行。

如果沒有安裝Qt qmake,可以使用apt-get來獲取(大便系的linux)

o@o-pc:~/MyStudy$ sudo apt-get install qt5-qmake 

如果你加了,使用下面的命令來生成Makefile文件

qmake hello.pro -o Makefile

其實這樣也可以(qtchooser在/usr/bin目錄下,這里的qt是安裝到默認目錄的)

o@o-pc:~/hello$ qtchooser -run-tool=qmake -qt=5 hello.pro 

我這里是這樣的

o@o-pc:~/program_files/Qt5.4.1/5.4/gcc_64/bin$ ./qmake ~/hello/hello.pro -o ~/hello/Makefile

編譯hello程序

生成了makefile之后,使用make命令來編譯就可以了。
可以看到我這里出了一點問題,報了一堆錯誤。是因為沒有鏈接xcb這個庫。這是在我安裝了xcb庫之后依然存在的錯誤,最開始是找不到libGL這個問題。解決的辦法就是修改.pro文件。這個見后面

o@o-pc:~/hello$ make g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o main.o main.cpp g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o widget.o widget.cpp /usr/lib/x86_64-linux-gnu/qt5/bin/moc -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I/usr/include/c++/4.8 -I/usr/include/x86_64-linux-gnu/c++/4.8 -I/usr/include/c++/4.8/backward -I/usr/lib/gcc/x86_64-linux-gnu/4.8/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include widget.h -o moc_widget.cpp g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o moc_widget.o moc_widget.cpp g++ -m64 -Wl,-O1 -o hello main.o widget.o moc_widget.o -L/usr/X11R6/lib64 -lQt5Widgets -L/usr/lib/x86_64-linux-gnu -lQt5Gui -lQt5Core -lGL -lpthread /usr/lib/x86_64-linux-gnu/libGL.so:對‘xcb_poll_for_special_event’未定義的引用 /usr/lib/x86_64-linux-gnu/libGL.so:對‘xcb_wait_for_special_event’未定義的引用 //usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0:對‘xcb_get_reply_fds’未定義的引用 /usr/lib/x86_64-linux-gnu/libGL.so:對‘xcb_unregister_for_special_event’未定義的引用 /usr/lib/x86_64-linux-gnu/libGL.so:對‘xcb_register_for_special_xge’未定義的引用 //usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0:對‘xcb_send_fd’未定義的引用 collect2: error: ld returned 1 exit status make: *** [hello] 錯誤 1 

解決 xcb_xxx未定義的引用的問題

這里出現的都是xcb_xxx...未定義的引用的問題,說明在鏈接的時候沒有鏈接到libxcb庫。很好解決,修改pro文件中的LIBS變量就是。

# 使用到的Qt庫 QT += core widgets #目標文件名 TARGET = hello #生成應用程序 TEMPLATE = app #用到的cpp源文件 SOURCES += main.cpp hello.cpp #用到的cpp頭文件 HEADERS += hello.h #解決 xcb_xxx未定義引用問題 LIBS += -lxcb

未出錯的編譯

下面可以直接使用qmake是因為我安裝了qt5-qmake(sudo apt-get install qt5-qmake)

o@o-pc:~/hello$ ls hello.cpp hello.h hello.pro hello.pro.user main.cpp o@o-pc:~/hello$ qmake o@o-pc:~/hello$ make g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o main.o main.cpp g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o hello.o hello.cpp /usr/lib/x86_64-linux-gnu/qt5/bin/moc -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I/usr/include/c++/4.8 -I/usr/include/x86_64-linux-gnu/c++/4.8 -I/usr/include/c++/4.8/backward -I/usr/lib/gcc/x86_64-linux-gnu/4.8/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include hello.h -o moc_hello.cpp g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o moc_hello.o moc_hello.cpp g++ -m64 -Wl,-O1 -o hello main.o hello.o moc_hello.o -L/usr/X11R6/lib64 -lxcb -lQt5Widgets -L/usr/lib/x86_64-linux-gnu -lQt5Gui -lQt5Core -lGL -lpthread o@o-pc:~/hello$ 

http://www.cnblogs.com/oloroso/p/4595181.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM