Qt:PRO 到 CMake 的转换——AutoMoc 子进程错误

Posted

技术标签:

【中文标题】Qt:PRO 到 CMake 的转换——AutoMoc 子进程错误【英文标题】:Qt: PRO to CMake conversion -- AutoMoc subprocess error 【发布时间】:2020-06-02 13:48:41 【问题描述】:

我是 Qt 新手,我正在尝试将 .pro 文件转换为 CMakeLists.text 将项目与 CMake 一起使用。

操作系统: Windows 10

.pro 文件:

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
    main.cpp \
    mainwindow.cpp \
    parallelogrambutton.cpp \
    rombusbutton.cpp \
    scrollarea.cpp \
    skewedbutton.cpp \
    trapezbutton.cpp

HEADERS += \
    mainwindow.h \
    parallelogrambutton.h \
    rombusbutton.h \
    scrollarea.h \
    skewedbutton.h \
    trapezbutton.h

# Default rules for deployment.
qnx: target.path = /tmp/$$TARGET/bin
else: unix:!android: target.path = /opt/$$TARGET/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    Resources.qrc

手动转换为CMakeText.txt的结果

cmake_minimum_required(VERSION 3.15)

project(Project)

set(CMAKE_PREFIX_PATH "C:/Qt/5.14.1/mingw73_64")
set(CMAKE_INCLUDE_PATH "C:/Qt/5.14.1/mingw73_64/bin")

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5 COMPONENTS Core Gui REQUIRED)

qt5_add_resources(Resources.qrc)

set(SOURCES
        main.cpp
        mainwindow.cpp
        parallelogrambutton.cpp
        rombusbutton.cpp
        scrollarea.cpp
        skewedbutton.cpp
        trapezbutton.cpp
        )

set(HEADERS
        mainwindow.h
        parallelogrambutton.h
        rombusbutton.h
        scrollarea.h
        skewedbutton.h
        trapezbutton.h
        )

add_executable($PROJECT_NAME $SOURCES $HEADERS)

qt5_use_modules($PROJECT_NAME Core Gui)

但是当我尝试运行这个 CMake 文件时,我收到以下错误:

错误信息:

/<CMAKE_PATH>\cmake.exe --build /<PATH>/cmake-build-debug --target Project -- -j 4
[ 10%] Automatic MOC and UIC for target Project

AutoMoc subprocess error
------------------------
The moc process failed to compile
  "/<PATH>/mainwindow.h"
into
  "/<PATH>/cmake-build-debug/Project_autogen/EWIEGA46WW/moc_mainwindow.cpp".

Command
-------
/<CMAKE_PREFIX_PATH>/bin/moc.exe -I/<PATH>/cmake-build-debug -I/<PATH> -I/<CMAKE_PREFIX_PATH>/include -I/<CMAKE_PREFIX_PATH>/include/QtCore -I/<CMAKE_PREFIX_PATH>/./mkspecs/win32-g++ -I/<CMAKE_PREFIX_PATH>/include/QtGui -I/<CMAKE_PREFIX_PATH>/include/QtANGLE -I/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++ -I/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin -I/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward -I/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include -I/usr/include -I/usr/include/w32api -DQT_CORE_LIB -DQT_GUI_LIB --include /<PATH>/cmake-build-debug/Project_autogen/moc_predefs.h -o /<PATH>/cmake-build-debug/Project_autogen/EWIEGA46WW/moc_mainwindow.cpp /<PATH>/mainwindow.h

Output
------

make[3]: *** [CMakeFiles/Project_autogen.dir/build.make:58: CMakeFiles/Project_autogen] Error 1
make[2]: *** [CMakeFiles/Makefile2:104: CMakeFiles/Project_autogen.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/Project.dir/rule] Error 2
make: *** [Makefile:118: Project] Error 2

谁能向我解释一下我在这里做错了什么?

【问题讨论】:

此处question 出现类似错误,但尚未发布解决方案。 【参考方案1】:

您似乎正在使用 Qt 的过时 CMake 命令,请参阅 https://doc.qt.io/qt-5/cmake-command-reference.html

您可以在 Qt Creator 中创建一个新的基于 CMake 的项目并将其作为基础。 Qt Creator v4.12.x 将生成如下内容:

cmake_minimum_required(VERSION 3.15)

project(Project)

set(CMAKE_PREFIX_PATH "C:/Qt/5.14.1/mingw73_64")
set(CMAKE_INCLUDE_PATH "C:/Qt/5.14.1/mingw73_64/bin")

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5 COMPONENTS Core Gui REQUIRED)

set(SOURCES
    main.cpp
    Resources.qrc
    mainwindow.cpp
    parallelogrambutton.cpp
    rombusbutton.cpp
    scrollarea.cpp
    skewedbutton.cpp
    trapezbutton.cpp
)

set(HEADERS
    mainwindow.h
    parallelogrambutton.h
    rombusbutton.h
    scrollarea.h
    skewedbutton.h
    trapezbutton.h
)

add_executable($PROJECT_NAME $SOURCES $HEADERS)

target_link_libraries($PROJECT_NAME PRIVATE Qt5::Core Qt5::Gui)

【讨论】:

以上是关于Qt:PRO 到 CMake 的转换——AutoMoc 子进程错误的主要内容,如果未能解决你的问题,请参考以下文章

Qt之QMake编译转换为CMake编译

cmake 不使用 QT4_automoc 生成 Qt moc 文件

qt将数据库sql添加到从cmake导入的项目中

将 it++ 与 cmake ( QT ) 一起使用

Wt 与 Qt,.pro 设置

如何在 CMake 文件中添加定义 QT_NO_DEBUG_OUTPUT?