gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:36:51 24 4
gpt4 key购买 nike

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

去年关闭。




Improve this question




我在使用 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 .对于构建问题,我添加了缺少的 Widgets :

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)

关于c++ - 带有 QT5 "Qt5::QtCore"的现代 CMake 3 但未找到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59862653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com