미리 컴파일된 헤더 사용하기
사전 컴파일된 헤더(PCH)는 일부 컴파일러에서 안정적인 코드 본문을 컴파일하고 컴파일된 코드 상태를 바이너리 파일에 저장하기 위해 지원하는 성능 기능입니다. 이후 컴파일하는 동안 컴파일러는 저장된 상태를 로드하고 지정된 파일을 계속 컴파일합니다. 안정된 코드를 다시 컴파일할 필요가 없으므로 이후의 각 컴파일이 더 빠릅니다.
qmake는 다음을 포함한 일부 플랫폼 및 빌드 환경에서 미리 컴파일된 헤더 사용을 지원합니다:
- Windows
- nmake
- Visual Studio 프로젝트(VS 2008 이상)
- macOS, iOS, tvOS 및 watchOS
- 메이크파일
- Xcode
- Unix
- GCC 3.4 이상
- clang
프로젝트에 미리 컴파일된 헤더 추가하기
사전 컴파일된 헤더에는 프로젝트 전체에 걸쳐 안정적이고 정적인 코드가 포함되어야 합니다. 일반적인 사전 컴파일된 헤더는 다음과 같습니다:
// 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
참고: C 파일용 미리 컴파일된 헤더 파일에는 C++ 코드가 포함되지 않을 수 있으므로 미리 컴파일된 헤더 파일은 C 포함과 C++ 포함을 구분해야 합니다.
프로젝트 옵션
프로젝트에서 미리 컴파일된 헤더를 사용하도록 하려면 프로젝트 파일에 PRECOMPILED_HEADER 변수만 정의하면 됩니다:
PRECOMPILED_HEADER = stable.h
나머지는 미리 컴파일된 헤더 파일의 생성 및 사용을 보장하기 위해 qmake가 처리합니다. 구성이 미리 컴파일된 헤더를 지원하는 경우 qmake가 이 작업을 수행하므로 HEADERS
에 미리 컴파일된 헤더 파일을 포함할 필요가 없습니다.
Windows를 대상으로 하는 MSVC 및 g++ 사양은 기본적으로 precompile_header
을 활성화합니다.
이 옵션을 사용하면 프로젝트 파일에서 조건부 블록을 트리거하여 미리 컴파일된 헤더를 사용할 때 설정을 추가할 수 있습니다. 예를 들어
precompile_header:!isEmpty(PRECOMPILED_HEADER) { DEFINES += USING_PCH }
MSVC nmake 대상의 C 파일에도 미리 컴파일된 헤더를 사용하려면 CONFIG 변수에 precompile_header_c
을 추가합니다. 헤더가 C++에도 사용되며 C++ 키워드/인클루드가 포함된 경우 #ifdef __cplusplus
)로 묶습니다.
발생 가능한 문제에 대한 참고 사항
일부 플랫폼에서는 미리 컴파일된 헤더 파일의 파일 이름 접미사가 다른 객체 파일의 접미사와 동일합니다. 예를 들어, 다음 선언으로 인해 이름이 같은 두 개의 다른 객체 파일이 생성될 수 있습니다:
PRECOMPILED_HEADER = window.h SOURCES = window.cpp
이와 같은 잠재적인 충돌을 방지하려면 미리 컴파일할 헤더 파일에 고유한 이름을 지정하세요.
예제 프로젝트
다음 소스 코드는 Qt 배포판의 examples/qmake/precompile
디렉터리에서 찾을 수 있습니다:
mydialog.ui
다음 이미지는 Qt Creator 디자인 모드에서 mydialog.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
TEMPLATE = app LANGUAGE = C++ CONFIG += cmdline 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
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.