Qt5, CMake, AUTOMOC and precompiled headers

How to point a precompiled header to CMake (2.8.12.1) AUTOMOC output?

So far in CMakeLists.txt, I have tried these two separately:

set(AUTOMOC_MOC_OPTIONS "-bstdafx.h") set(AUTOMOC_MOC_OPTIONS "-fstdafx.h") 

The generated AUTOMOC output when creating the project (project_automoc.cpp) contains only the moc_xxx.cpp files:

 /* This file is autogenerated, do not edit*/ /// <- stdafx.h should be here ?!?! #include "moc_widget_fps.cpp" #include "moc_widget_sysevents.cpp" 
+6
source share
3 answers

The correct variable for the set is called CMAKE_AUTOMOC_MOC_OPTIONS . It is used to initialize the AUTOMOC_MOC_OPTIONS property of AUTOMOC_MOC_OPTIONS object, i.e.:

 set (CMAKE_AUTOMOC_MOC_OPTIONS "-bstdafx.h" "-fstdafx.h") 

Also note that this will only cause the Qt MOC compiler to add the data included in each generated moc_xxx.cpp file. The general project_automoc.cpp will not be affected.

+2
source

After some digging, I decided to just turn off the AUTOMOC function for projects that use precompiled headers:

 set_target_properties (ProjectName PROPERTIES AUTOMOC FALSE) # Set the headers that need moc'ing file (GLOB MOC_FILES widget_filetransfer.h widget_main_menu.h widget_main_toolbar.h) QT5_WRAP_CPP (MOC_SOURCES ${MOC_FILES}) ... # Force PCH for the generated MOC files foreach (src_file ${MOC_SOURCES}) set_source_files_properties (${src_file} PROPERTIES COMPILE_FLAGS "/Yustdafx.h /FIstdafx.h" ) endforeach() 
+2
source

AUTOMOC_MOC_OPTIONS does not affect the project_automoc.cpp file. It contains the parameters passed to moc to create "moc_widget_fps.cpp" and "moc_widget_sysevents.cpp". They should contain your pch.

0
source

All Articles