与一个大四的学生同做一个项目,源文件有几十个,刚开始还在为写makefile发愁,幸好找到了automake工具链,觉得真是省了不少事。
后来他跟我说他用的是qmake,比用automake更省了不少事。一比较,发现qmake确实是一个更好地选择。
automake工具链比较复杂。一般要autoscan+修改configure.in+aclocal+写Makefile.am+configure+make。很不容易上手,而且
容易出错。automake提供了更为精细的控制,复杂但强大。但其实一般我们也用不了那么多的功能,而且中间生成的那些配置文件也不在
少数。这时候还要将源码与makefile部分分离开来。一般的开源软件就采用这种方法,文件层次清楚,易于管理。但是一般的很少的文件
的话,我觉得用qmake就足够了,易于上手,也不易出错。
qmake一般是qmake -project/qmake/make三步,中间需要适当地修改生成的*.pro文件,然后就差不多了。qmake -project主要就是用
来生成*.pro文件。主要做的就是读入当前目录的源文件。一些常用的配置变量如下:
Variable | Contents |
---|
CONFIG | General project configuration options. |
DESTDIR | The directory in which the executable or binary file will be placed. |
FORMS | A list of UI files to be processed by uic. |
HEADERS | A list of filenames of header (.h) files used when building the project. |
QT | Qt-specific configuration options. |
RESOURCES | A list of resource (.rc) files to be included in the final project. See the The Qt Resource System for more information about these files. |
SOURCES | A list of source code files to be used when building the project. |
TEMPLATE | The template to use for the project. This determines whether the output of the build process will be an application, a library, or a plugin. |
变量之间可以相互赋值比如。
TEMP_SOURCES = $$SOURCES
Template变量比较重要:用来决定所建工程的类型:
Template | Description of qmake output |
---|
app (default) | Creates a Makefile to build an application. |
lib | Creates a Makefile to build a library. |
subdirs | Creates a Makefile containing rules for the subdirectories specified using the SUBDIRSvariable. Each subdirectory must contain its own project file. |
vcapp | Creates a Visual Studio Project file to build an application. |
vclib | Creates a Visual Studio Project file to build a library. |
vcsubdirs | Creates a Visual Studio Solution file to build projects in sub-directories. |
一般用的都是app。
再说上面的config变量,内置的有:
Option | Description |
---|
release | The project is to be built in release mode. This is ignored if debug is also specified. |
debug | The project is to be built in debug mode. |
debug_and_release | The project is built in both debug and release modes. |
debug_and_release_target | The project is built in both debug and release modes. TARGET is built intoboth the debug and release directories. |
build_all | If debug_and_release is specified, the project is built in both debug and release modes by default. |
autogen_precompile_source | Automatically generates a .cpp file that includes the precompiled header file specified in the .pro file. |
ordered | When using the subdirs template, this option specifies that the directories listed should be processed in the order in which they are given. |
warn_on | The compiler should output as many warnings as possible. This is ignored ifwarn_off is specified. |
warn_off | The compiler should output as few warnings as possible. |
copy_dir_files | Enables the install rule to also copy directories, not just files. |
debug_and_release建立两个版本。config还可以加上qt,thread,X11等用来指定建立相应的工程类型。
如果是qt工程的话,QT变量就是一个需要关注的对象。当用到相应的库时,就要加上相应的名字比如:
常用的就是core,gui.其他的用到时加上就行。
另外一个比较重要问题就是QT与其他库的融合,LIBS和INCLUDEPATH可以做到这一点。看名字就知道其作用。
和Gcc命令行参数的用法类似。
qmake也有一些内置的函数用来提供更为强大的功能。常用到的有include,可以用来包含其他的工程文件,类似于C的头文件。还有条件控制类的,如:
win32 { SOURCES += paintwidget_win.cpp }
win32就是一个条件。甚至还有循环控制语句:
EXTRAS = handlers tests docs for(dir, EXTRAS) { exists($$dir) { SUBDIRS += $$dir } }
这样算下来,其实qmake也挺复杂的,不过相对于automake来说,还是很容易的。