简单例子linux
假设已经实现以下程序:编程
hello.cpp
hello.h
main.cppwindows
首先,使用编辑器,在上述文件目录下建立文件hello.pro.而后加入几行语句告诉qmake项目中的源文件和头文件。编辑器
使用 SOURCES 变量 加入源文件,例如:函数
SOURCES +=hello.cpp
加入全部源文件测试
SOURCES += hello.cpp
SOURCES += main.cpp
也可使用Make-like语法形式。以下:ui
SOURCES = hello.cpp\
main.cpp
加入头文件spa
HEADERS += hello.h SOURCES +=hello.cpp SOURCES += main.cpp
target 是自动设定的;就像project file 同样。可是不一样的平台有不一样的后缀。好比,若是project file 是 hello.pro,那么在Windows上target就是 hello.exe,而linux上则是 hello.若是使用不一样的名称的话,能够这样设置project file:命令行
TARGET = helloworld
设置 CONFIG变量的最后一步。因为是QT应用程序,必须把qt放入CONFIG中,这样qmake就会生成相应的连接库,并确保创建的moc和uic包含在生成的Makefile中。debug
最终的project file应该像这样:
CONFIG +=qt HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp
如今就可使用qmake来为应用程序生成Makefile。在项目目录下,输入命令行,以下:
qmake -o Makefile hello.pro
而后根据使用的编译器输入make和nmake
对于Visual Studio用户来讲,qmake也会生成.dsp和.vcproj文件,好比:
qmake -tp vc hello.pro
使应用程序可调试
release版本的应用程序不包含任何调试符号或者其余的调试信息。在开发过程当中每每须要调试版本提供相关的信息。这个很容易实现,只要在CONFIG变量中加入debug
例如:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp
像以前同样使用qmake产生Makefile,能够在debugging环境下,运行程序,获得有用的信息。
加入特定平台的源文件
在经过几个小时的编程后,有可能开始写特定平台部分,并且须要与其余代码独立分开。有两个新文件:hellowin.cpp和hellolinux.cpp.不能把它们都加入SOURCES变量中,这样两个文件都会在Makefile中。因此在运行qmake时,须要使用一个范围根据独立平台来处理。
一个适用于windows的简单范围,以下:
win32{ SOURCES += hellowin.cpp }
若是qmake运行在Windows上,将hellowin.cpp加入到源文件列表中。若是qmake运行在其余平台上,就会被忽略。
如今剩下的就是为Unix-specific 文件建立一个范围,以下:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp }
像以前同样使用qmake生成Makefile
若是文件不存在中止qmake
若是某些文件不存在,可能就不想建立Makefile.可使用exists()函数来检查文件是否存在。使用error()函数来中止qmake.这和scopes的效果是同样的。简单的使用函数来代替scope 条件
检查main.cpp是否存在,以下:
!exists(main.cpp) { errror("No main.cpp file found") }
!符号用来否认测试;例如,exists(main.cpp) 返回 true 则文件存在,若是 !exists(main.cpp)返回true,则文件不存在。
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp } !exists(main.cpp){ error("No main.cpp file found") }
像以前同样生成makefile,若是重命名main.cpp,将会看到一些信息而且qmake会中止。
多条件检查
假设使用Windows ,在命令行运行应用程序时,但愿能看到qDebug()的声明输出。若是构建的应用程序没有合适的控制设置,就不能看到输出。只要在CONFIG中加入console,makefile中就会有设置。然而,若是debug已经加入了CONFIG中,想在运行时,加入console。须要使用嵌套范围,例如:
win32 { debug { CONFIG +=console } }
嵌套范围使用冒号连在一块儿,最终的项目以下:
CONFIG += qt debug HEADERS += hello.h SOURCES +=hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp } !exists(main.cpp){ error("No main.cpp file found") } win32:debug { CONFIG += console }