Qt:Документация 4.3.2/qmake-precompiledheaders

Материал из Wiki.crossplatform.ru

Версия от 10:24, 29 января 2009; Root (Обсуждение | вклад)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск
40px Внимание: Актуальная версия перевода документации находится здесь

__NOTOC__

Image:qt-logo.png

Главная · Все классы · Основные классы · Классы по группам · Модули · Функции

Image:trolltech-logo.png

[Previous: qmake Advanced Usage ] [ Contents ] [Next: qmake Reference ]

Содержание

[править] Использование предварительно скомпилированных заголовков

Precompiled headers are a performance feature supported by some compilers to compile a stable body of code, and store the compiled state of the code in a binary file. During subsequent compilations, the compiler will load the stored state, and continue compiling the specified file. Each subsequent compilation is faster because the stable code does not need to be recompiled.

qmake supports the use of precompiled headers (PCH) on some platforms and build environments, including:

  • Windows
    • nmake
    • Dsp projects (VC 6.0)
    • Vcproj projects (VC 7.0 & 7.1)
  • Mac OS X
    • Makefile
    • Xcode
  • Unix
    • GCC 3.4 and above

[править] Добавление предварительно скомпилированных заголовков в ваш проект

[править] Содержимое файла предварительно скомпилированных заголовков

The precompiled header must contain code which is stable and static throughout your project. A typical PCH might look like this:

[править] Пример: stable.h

 // Add C includes here
 
 #if defined __cplusplus
 // Add C++ includes here
 #include <stdlib>
 #include <iostream>
 #include <vector>
 #include <QApplication> // Qt includes
 #include <QPushButton>
 #include <QLabel>
 #include "thirdparty/include/libmain.h"
 #include "my_stable_class.h"
 ...
 #endif

Note that a precompiled header file needs to separate C includes from C++ includes, since the precompiled header file for C files may not contain C++ code.

[править] Опции проекта

To make your project use PCH, you only need to define the PRECOMPILED_HEADER variable in your project file:

     PRECOMPILED_HEADER = stable.h

qmake will handle the rest, to ensure the creation and use of the precompiled header file. You do not need to include the precompiled header file in HEADERS, as qmake will do this if the configuration supports PCH.

All platforms that support precompiled headers have the configuration option precompile_header set. Using this option, you may trigger conditional blocks in your project file to add settings when using PCH. Пример:

     precompile_header:!isEmpty(PRECOMPILED_HEADER) {
     DEFINES += USING_PCH
     }

[править] Примечания по возможным решениям

On some platforms, the file name suffix for precompiled header files is the same as that for other object files. For example, the following declarations may cause two different object files with the same name to be generated:

     PRECOMPILED_HEADER = window.h
     SOURCES            = window.cpp

To avoid potential conflicts like these, it is good practice to ensure that header files that will be precompiled are given distinctive names.

[править] Пример проекта

You can find the following source code in the examples/qmake/precompile directory in the Qt distribution:

[править] mydialog.ui

 <ui version="4.0" >
  <author></author>
  <comment></comment>
  <exportmacro></exportmacro>
  <class>MyDialog</class>
  <widget class="QDialog" name="MyDialog" >
   <property name="geometry" >
    <rect>
     <x>0</x>
     <y>0</y>
     <width>401</width>
     <height>70</height>
    </rect>
   </property>
   <property name="windowTitle" >
    <string>Mach 2!</string>
   </property>
   <layout class="QVBoxLayout" >
    <property name="margin" >
     <number>9</number>
    </property>
    <property name="spacing" >
     <number>6</number>
    </property>
    <item>
     <widget class="QLabel" name="aLabel" >
      <property name="text" >
       <string>Join the life in the fastlane; - PCH enable your project today! -</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="aButton" >
      <property name="text" >
       <string>&amp;amp;Quit</string>
      </property>
      <property name="shortcut" >
       <string>Alt+Q</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
  <resources/>
  <connections/>
 </ui>

[править] stable.h

 /* Add C includes here */
 
 #if defined __cplusplus
 /* Add C++ includes here */
 
 # include <iostream>
 # include <QApplication>
 # include <QPushButton>
 # include <QLabel>
 #endif

[править] myobject.h

 #include <QObject>
 
 class MyObject : public QObject
 {
 public:
     MyObject();
     ~MyObject();
 };

[править] myobject.cpp

 #include <iostream>
 #include <QDebug>
 #include <QObject>
 #include "myobject.h"
 
 MyObject::MyObject()
     : QObject()
 {
     std::cout << "MyObject::MyObject()\n";
 }

[править] util.cpp

 void util_function_does_nothing()
 {
     // Nothing here...
     int x = 0;
     ++x;
 }

[править] main.cpp

 #include <QApplication>
 #include <QPushButton>
 #include <QLabel>
 #include "myobject.h"
 #include "mydialog.h"
 
 int main(int argc, char **argv)
 {
     QApplication app(argc, argv);
 
     MyObject obj;
     MyDialog dialog;
 
     dialog.connect(dialog.aButton, SIGNAL(clicked()), SLOT(close()));
     dialog.show();
 
     return app.exec();
 }

[править] precompile.pro

 #############################################
 #
 # Example for using Precompiled Headers
 #
 #############################################
 TEMPLATE  = app
 LANGUAGE  = C++
 CONFIG   += console precompile_header
 
 # Use Precompiled headers (PCH)
 PRECOMPILED_HEADER  = stable.h
 
 HEADERS   = stable.h \
             mydialog.h \
             myobject.h
 SOURCES   = main.cpp \
             mydialog.cpp \
             myobject.cpp \
             util.cpp
 FORMS     = mydialog.ui

[Previous: qmake Advanced Usage ] [ Contents ] [Next: qmake Reference ]



Copyright © 2007 Trolltech Trademarks
Qt 4.3.2