带有 QT5 "Qt5::QtCore" 的现代 CMake 3 但未找到目标

Modern CMake 3 with QT5 "Qt5::QtCore" but the target was not found

我在使用 CMake 和一个简单的 QT 示例时遇到问题。我正在更新我的 CMake 配置以遵循 modern way of doing it, meaning supporting CMake > v3.0.

这是我的 CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project("test")

set(CMAKE_AUTOMOC ON)
find_package(Qt5 COMPONENTS Core REQUIRED)
message(STATUS "Qt5 version: ${Qt5_VERSION}")
get_target_property(QtCore_location Qt5::Core LOCATION)
message(STATUS "Qt5 version: ${QtCore_location}")

add_executable(Test main.cpp)
target_link_libraries(Test Qt5::QtCore)

我的代码示例 (main.cpp)

#include <QApplication>
#include <QProgressBar>
#include <QSlider>

int main(int argc, char **argv)
{
 QApplication app (argc, argv);

 // Create a container window
 QWidget window;
 window.setFixedSize(200, 80);

 // Create a progress bar
 // with the range between 0 and 100, and a starting value of 0
 QProgressBar *progressBar = new QProgressBar(&window);
 progressBar->setRange(0, 100);
 progressBar->setValue(0);
 progressBar->setGeometry(10, 10, 180, 30);

 // Create a horizontal slider
 // with the range between 0 and 100, and a starting value of 0
 QSlider *slider = new QSlider(&window);
 slider->setOrientation(Qt::Horizontal);
 slider->setRange(0, 100);
 slider->setValue(0);
 slider->setGeometry(10, 40, 180, 30);

 window.show();

 // Connection
 // This connection set the value of the progress bar
 // while the slider's value changes
 QObject::connect(slider, SIGNAL (valueChanged(int)), progressBar, SLOT (setValue(int)));

 return app.exec();
}

但是当我 运行 CMake 时出现以下错误:

build# cmake ..
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Qt5 version: 5.12.3
-- Qt5 version: /opt/Qt5.12.3/5.12/gcc_64/lib/libQt5Core.so.5.12.3
-- Configuring done
CMake Error at CMakeLists.txt:16 (add_executable):
  Target "Test" links to target "Qt5::QtCore" but the target was not found.
  Perhaps a find_package() call is missing for an IMPORTED target, or an
  ALIAS target is missing?


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

我不明白我错过了什么。我关注了 cmake documentation example

感谢您的帮助,

编辑 1: 感谢您的评论,我修复了拼写错误问题。这是 Qt5::Core 而不是 Qt5::QTCore。 但现在我遇到了这个构建问题:

Scanning dependencies of target Test_autogen
[ 25%] Automatic MOC for target Test
[ 25%] Built target Test_autogen
Scanning dependencies of target Test
[ 50%] Building CXX object CMakeFiles/Test.dir/Test_autogen/mocs_compilation.cpp.o
[ 75%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
/app/build/main.cpp:1:10: fatal error: QApplication: No such file or directory
 #include <QApplication>
          ^~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/Test.dir/build.make:75: recipe for target 'CMakeFiles/Test.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/Test.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:76: recipe for target 'CMakeFiles/Test.dir/all' failed
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

解决方案基于评论: 感谢您的评论,我修复了拼写错误问题。这是 Qt5::Core 而不是 Qt5::QTCore。对于构建问题,我添加了缺少的小部件:

cmake_minimum_required(VERSION 3.16)

project("test")

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)

add_executable(Test main.cpp)
target_link_libraries(Test Qt5::Core Qt5::Widgets)