子弹 の VISIONS

NEVER back down ~~

C++博客 首页 新随笔 联系 聚合 管理
  112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks
qmake Project Files

Project files contain all the information required by qmake to build your application, library, or plugin. The resources used by your project are generally specified using a series of declarations, but support for simple programming constructs allow you to describe different build processes for different platforms and environments.

Project File Elements

The project file format used by qmake can be used to support both simple and fairly complex build systems. Simple project files will use a straightforward declarative style, defining standard variables to indicate the source and header files that are used in the project. Complex projects may use the control flow structures to fine-tune the build process.

The following sections describe the different types of elements used in project files.

Variables

In a project file, variables are used to hold lists of strings. In the simplest projects, these variables inform qmake about the configuration options to use, or supply filenames and paths to use in the build process.

qmake looks for certain variables in each project file, and it uses the contents of these to determine what it should write to a Makefile. For example, the list of values in the HEADERS and SOURCES variables are used to tell qmake about header and source files in the same directory as the project file.

Variables can also be used internally to store temporary lists of values, and existing lists of values can be overwritten or extended with new values.

The following lines show how lists of values are assigned to variables:

 HEADERS = mainwindow.h paintwidget.h 

Note that the first assignment only includes values that are specified on the same line as the SOURCES variable. The second assignment splits the items across lines by using the \\ character.

The list of values in a variable is extended in the following way:

 SOURCES = main.cpp mainwindow.cpp \
           paintwidget.cpp
 CONFIG += qt 

The CONFIG variable is another special variable that qmake uses when generating a Makefile. It is discussed in the section on general configuration later in this chapter. In the above line, qt is added to the list of existing values contained in CONFIG.

The following table lists the variables that qmake recognizes, and describes what they should contain.

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.

The contents of a variable can be read by prepending the variable name with $$. This can be used to assign the contents of one variable to another:

 TEMP_SOURCES = $$SOURCES 

The $$ operator is used extensively with built-in functions that operate on strings and lists of values. These are described in the chapter on qmake Advanced Usage.

Normally, variables are used to contain whitespace-separated lists of values. However, it is sometimes necessary to specify values containing spaces. These must be quoted in the following way:

 DEST = "Program Files" 

The quoted text is treated as a single item in the list of values held by the variable.

Comments

You can add comments to project files. Comments begin with the # character and continue to the end of the same line. For example:

 # Comments usually start at the beginning of a line, but they
 # can also follow other content on the same line. 

To include the # character in variable assignments, it is necessary to use the contents of the built-in LITERAL_HASH variable. See the variable reference for more information.

Built-in Functions and Control Flow

qmake provides a number of built-in functions to allow the contents of variables to be processed. The most commonly used function in simple project files is the include function which takes a filename as an argument. The contents of the given file are included in the project file at the place where the include function is used. The include function is most commonly used to include other project files:

 include(other.pro) 

Support for conditional structures is made available via scopes that behave like if statements in programming languages:

 win32 {
     SOURCES += paintwidget_win.cpp
 } 

The assignments inside the braces are only made if the condition is true. In this case, the special win32 variable must be set; this happens automatically on Windows, but this can also be specified on other platforms by running qmake with the -win32 command line option (see Running qmake for more information). The opening brace must stand on the same line as the condition.

Simple loops are constructed by iterating over lists of values using the built-in for function. The following code adds directories to the SUBDIRS variable, but only if they exist:

 EXTRAS = handlers tests docs
 for(dir, EXTRAS) {
     exists($$dir) {
         SUBDIRS += $$dir
     }
 } 

More complex operations on variables that would usually require loops are provided by built-in functions such as find, unique, and count. These functions, and many others are provided to manipulate strings and paths, support user input, and call external tools. A list of the functions available can be found in the qmake Advanced Usage chapter of this manual.

Project Templates

The TEMPLATE variable is used to define the type of project that will be built. If this is not declared in the project file, qmake assumes that an application should be built, and will generate an appropriate Makefile (or equivalent file) for the purpose.

The types of project available are listed in the following table with information about the files that qmake will generate for each of them:

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 SUBDIRS variable. 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.

See the qmake Tutorial for advice on writing project files for projects that use the app and lib templates.

When the subdirs template is used, qmake generates a Makefile to examine each specified subdirectory, process any project file it finds there, and run the platform's make tool on the newly-created Makefile. The SUBDIRS variable is used to contain a list of all the subdirectories to be processed.

General Configuration

The CONFIG variable specifies the options and features that the compiler should use and the libraries that should be linked against. Anything can be added to the CONFIG variable, but the options covered below are recognized by qmake internally.

The following options control the compiler flags that are used to build the project:

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.

build_all

If debug_and_release is specified, the project is built in both debug and release modes by default.

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 if warn_off is specified.

warn_off

The compiler should output as few warnings as possible.

The debug_and_release option is special in that it enables both debug and release versions of a project to be built. In such a case, the Makefile that qmake generates includes a rule that builds both versions, and this can be invoked in the following way:

 make all 

Adding the build_all option to the CONFIG variable makes this rule the default when building the project, and installation targets will be created for both debug and release builds.

Note that each of the options specified in the CONFIG variable can also be used as a scope condition. You can test for the presence of certain configuration options by using the built-in CONFIG() function. For example, the following lines show the function as the condition in a scope to test whether only the opengl option is in use:

 CONFIG(opengl) {
     message(Building with OpenGL support.)
 } else {
     message(OpenGL support is not available.)
 } 

This enables different configurations to be defined for release and debug builds, and is described in more detail in the Scopes section of the Advanced Usage chapter of this manual.

The following options define the type of project to be built. Note that some of these options only take effect when used on the relevant platform. On other platforms, they have no effect.

Option

Description

qt

The project is a Qt application and should link against the Qt library. You can use the QT variable to control any additional Qt modules that are required by your application.

thread

The project is a multi-threaded application.

x11

The project is an X11 application or library.

When using application or library project templates, more specialized configuration options can be used to fine tune the build process. These are explained in details in the Common Projects chapter of this manual.

For example, if your application uses the Qt library and you want to build it as a multi-threaded application in debug mode, your project file will contain the following line:

 CONFIG += qt thread debug 

Note, that you must use "+=", not "=", or qmake will not be able to use Qt's configuration to determine the settings needed for your project.

Declaring Qt Libraries

If the CONFIG variable contains the qt value, qmake's support for Qt applications is enabled. This makes it possible to fine-tune which of the Qt modules are used by your application. This is achieved with the QT variable which can be used to declare the required extension modules. For example, we can enable the XML and network modules in the following way:

 CONFIG += qt
 QT += network xml 

Note that QT includes the core and gui modules by default, so the above declaration adds the network and XML modules to this default list. The following assignment omits the default modules, and will lead to errors when the application's source code is being compiled:

 QT = network xml # This will omit the core and gui modules. 

If you want to build a project without the gui module, you need to exclude it with the "-=" operator. By default, QT contains both core and gui, so the following line will result in a minimal Qt project being built:

 QT -= gui # Only the core module is used. 

The table below shows the options that can be used with the QT variable and the features that are associated with each of them:

Option

Features

core (included by default)

QtCore module

gui (included by default)

QtGui module

network

QtNetwork module

opengl

QtOpenGL module

sql

QtSql module

svg

QtSvg module

xml

QtXml module

qt3support

Qt3Support module

Note that adding the opengl option to the QT variable automatically causes the equivalent option to be added to the CONFIG variable. Therefore, for Qt applications, it is not necessary to add the opengl option to both CONFIG and QT.

Configuration Features

qmake can be set up with extra configuration features that are specified in feature (.prf) files. These extra features often provide support for custom tools that are used during the build process. To add a feature to the build process, append the feature name (the stem of the feature filename) to the CONFIG variable.

For example, qmake can configure the build process to take advantage of external libraries that are supported by pkg-config, such as the D-Bus and ogg libraries, with the following lines:

 CONFIG += link_pkgconfig
 PKGCONFIG += ogg dbus-1 

More information about features can be found in the Adding New Configuration Features section of the qmake Advanced Usage chapter.

Declaring Other Libraries

If you are using other libraries in your project in addition to those supplied with Qt, you need to specify them in your project file.

The paths that qmake searches for libraries and the specific libraries to link against can be added to the list of values in the LIBS variable. The paths to the libraries themselves can be given, or the familiar Unix-style notation for specifying libraries and paths can be used if preferred.

For example, the following lines show how a library can be specified:

 LIBS += -L/usr/local/lib -lmath 

The paths containing header files can also be specified in a similar way using the INCLUDEPATH variable.

For example, it is possible to add several paths to be searched for header files:

 INCLUDEPATH = c:/msdev/include d:/stl/include 

[Previous: Using qmake] [Contents] [Next: Running qmake]


Copyright © 2008 Trolltech

Trademarks

Qt 4.3.4

posted on 2008-08-04 12:00 子弹のVISIONS 阅读(1634) 评论(2)  编辑 收藏 引用 所属分类: 2.0 工作参考

Feedback

# re: qmake Project Files (.pro) 2008-08-04 12:08 子弹
 
QT中.pro文件的写法
2008-04-01 15:03
在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下:

1. 注释
从“#”开始,到这一行结束。

2. 指定源文件
SOURCES = *.cpp

对于多源文件,可用空格分开,如:SOURCES = 1.cpp 2.cpp 3.cpp
或者每一个文件可以被列在一个分开的行里面,通过反斜线另起一行,就像这样:

SOURCES = hello.cpp \
      main.cpp
一个更冗长的方法是单独地列出每一个文件,就像这样:

    SOURCES += hello.cpp
    SOURCES += main.cpp

这种方法中使用“+=”比“=”更安全,因为它只是向已有的列表中添加新的文件,而不是替换整个列表。

3. 指定头文件
HEADERS = hello.h或者HEADERS += hello.h

列出源文件的任何一个方法对头文件也都适用。

4. 配置信息
CONFIG用来告诉qmake关于应用程序的配置信息。

    CONFIG += qt warn_on release

在这里使用“+=”,是因为我们添加我们的配置选项到任何一个已经存在中。这样做比使用“=”那样替换已经指定的所有选项是更安全的。
A> qt部分告诉qmake这个应用程序是使用Qt来连编的。这也就是说qmake在连接和为编译添加所需的包含路径的时候会考虑到Qt库的。
B> warn_on部分告诉qmake要把编译器设置为输出警告信息的。
C> release部分告诉qmake应用程序必须被连编为一个发布的应用程序。在开发过程中,程序员也可以使用debug来替换release

5. 指定目标文件名
TARGET = filename

如果不设置该项目,目标名会被自动设置为跟项目文件一样的名称

6. 添加界面文件(ui)
INTERFACES = filename.ui

7. 平台相关性处理
我们在这里需要做的是根据qmake所运行的平台来使用相应的作用域来进行处理。为Windows平台添加的依赖平台的文件的简单的作用域看起来就像这样:

win32 {
SOURCES += hello_win.cpp
}

所以如果qmake运行在Windows上的时候,它就会把hello_win.cpp添加到源文件列表中。如果qmake运行在其它平台上的时候,它会很简单地把这部分忽略。

8. 如果一个文件不存在,停止qmake
如 果某一个文件不存在的时候,你也许不想生成一个Makefile。我们可以通过使用exists()函数来检查一个文件是否存在。我们可以通过使用 error()函数把正在运行的qmake停下来。这和作用域的工作方式一样。只要很简单地用这个函数来替换作用域条件。对main.cpp文件的检查就 像这样:

!exists( main.cpp ) {
   error( "No main.cpp file found" )
}

“!”用来否定这个测试,比如,如果文件存在,exists( main.cpp )是真,如果文件不存在,!exists( main.cpp )是真。

9. 检查多于一个的条件
假 设你使用Windows并且当你在命令行运行你的应用程序的时候你想能够看到qDebug()语句。除非你在连编你的程序的时候使用console设置, 你不会看到输出。我们可以很容易地把console添加到CONFIG行中,这样在Windows下,Makefile就会有这个设置。但是如果告诉你我 们只是想在当我们的应用程序运行在Windows下并且当debug已经在CONFIG行中的时候,添加console。这需要两个嵌套的作用域;只要生 成一个作用域,然后在它里面再生成另一个。把设置放在最里面的作用域里,就像这样:

win32 {
   debug {
     CONFIG += console
   }
}

嵌套的作用域可以使用冒号连接起来,像这样:

win32:debug {
CONFIG += console
}

10. 摸板
模板变量告诉qmake为这个应用程序生成哪种makefile。下面是可供使用的选择:

A> app - 建立一个应用程序的makefile。这是默认值,所以如果模板没有被指定,这个将被使用。
B> lib - 建立一个库的makefile。
C> vcapp - 建立一个应用程序的Visual Studio项目文件。
D> vclib - 建立一个库的Visual Studio项目文件。
E> subdirs - 这是一个特殊的模板,它可以创建一个能够进入特定目录并且为一个项目文件生成makefile并且为它调用make的makefile。

11. 生成Makefile
当你已经创建好你的项目文件,生成Makefile就很容易了,你所要做的就是先到你所生成的项目文件那里然后输入:

Makefile可以像这样由“.pro”文件生成:

    qmake -o Makefile hello.pro

对于Visual Studio的用户,qmake也可以生成“.dsp”文件,例如:

    qmake -t vcapp -o hello.dsp hello.pro
  回复  更多评论
  

# re: qmake Project Files (.pro) 2010-08-05 14:14 cosplay
I love your blog so much, and there are just some differences with others'. Hope there will be more wonderful things in your blog. .Happy every day!DXFD  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理