qmake的使用(可设置c编译器flag参数)

本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso
***
仍是先说一下当前的系统环境:Ubuntu 14.04 + Qt5.4
若是没有安装过QT,能够安装下面几个qt软件css

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函数链接信号的槽的时候,即使是槽函数不存在,也可以经过编译html

1. 先来写源代码

必须先写源代码,这个源代码很简单,就是建立一个Widget,而后widget上面有一个PushButton,点击以后弹出一个MessageBox来提示一个"hello world"linux

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)等。
这里并不打算详细介绍每个部分,只作简单介绍c++

.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

由于咱们这里比较简单,就只写须要的部分了。shell

# 使用到的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所提供的工具的主要连编工具。swift

qmake程序在Qt的安装目录下,好比个人机器上就在
o@o-pc:~/program_files/Qt5.4.1/5.4/gcc_64/bin$
你能够把这个路径加入到系统环境变量的PATH变量中,这样就能够随便使用了。我不喜欢加入到环境变量,因此这里指定路径来运行。app

若是没有安装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

相关文章
相关标签/搜索