首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >现代CMake 3与QT5“Qt5 5::QtCore”但目标未被找到

现代CMake 3与QT5“Qt5 5::QtCore”但目标未被找到
EN

Stack Overflow用户
提问于 2020-01-22 15:06:34
回答 1查看 2.5K关注 0票数 2

我对CMake和一个简单的QT示例有一个问题。我正在更新我的CMake配置,以遵循现代的方法,意思是支持CMake > v3.0。

这是我的CMakeLists.txt

代码语言:javascript
复制
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)

代码语言:javascript
复制
#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时,我会得到以下错误:

代码语言:javascript
复制
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文档示例

谢谢你的帮忙,

编辑1:谢谢你的评论,我解决了打字问题。它是Qt5::Core而不是Qt5::QTCore。但现在我明白了构建的问题:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-22 16:01:59

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

代码语言:javascript
复制
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)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59862653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档