I have a project that is being generated via CMake that also uses the AutoMOC flag. However, when I open the solution and try to build the project (using the Visual Studio 15 2017 x64 generator), this particular project fails. I'll post the error messages below (there are many), but most of them are 'struct' type redefinition errors or errors because of multiple initializations, etc. Unfortunately, because most of these errors are found in the .cpp files that are autogenerated by the moc, it's very difficult to debug (or know why they are happening in the first place).
Here's my CMakeList.txt:
set(target QUI)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets)
find_package(Qt5Network)
find_package(Qt5OpenGL)
find_package(Qt5PrintSupport)
if(WIN32)
find_package(Qt5WinExtras)
endif()
set( headers
# a bunch of headers here...
)
add_library(${target} "")
target_link_libraries(${target}
PUBLIC Events Reflection DynamicDispatch Qt5::Widgets Qt5::Network Qt5::OpenGL Qt5::PrintSupport
PRIVATE UI Net Registry
)
add_to_target( ${target} "${namespace}" "${headers}" "" )
generate_decl( ${target} "${namespace}" MLQ )
generate_ctags( ${tags_target} "${headers}" )
add_subdirectory( impl )
my_target_scope(${target})
And then this is a snippet of the errors that I'm seeing. In total, there are about 145 errors for this project.
Errors:
Error C2011 'qt_meta_stringdata_MainWindow_t': 'struct' type redefinition (compiling source file C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\mocs_compilation.cpp) C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\EWIEGA46WW\moc_MainWindow.cpp
Error C2374 'qt_meta_stringdata_MainWindow': redefinition; multiple initialization (compiling source file C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\mocs_compilation.cpp) C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\EWIEGA46WW\moc_MainWindow.cpp
Error C2027 use of undefined type 'qt_meta_stringdata_MainWindow_t' (compiling source file C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\mocs_compilation.cpp) C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\EWIEGA46WW\moc_MainWindow.cpp
Error C2227 left of '->stringdata0' must point to class/struct/union/generic type (compiling source file C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\mocs_compilation.cpp) C:\GIT\src\Apps\MyApp\Qt\MyApp_autogen\EWIEGA46WW\moc_MainWindow.cpp
So after a lot of troubleshooting (and comparing some files with colleagues who were able to get the solution building on their Windows machines)... we were able to determine that the issue boiled down to how CMake handled the AutoMOC process... I believe they made a change in CMake version 3.9 which caused the conflicts (see this thread). Apparently this was fixed in version 3.10... and once I updated to the latest version of CMake, my build process worked again. Hopefully this helps someone in the future.