我将Qt 6.2.1与cmake一起使用,但我找不到使用Qt图表的方法。
Qt图表安装在";C: \Qt\6.2.1 \Src\qtcharts">
在我的cmakelists.txt中,我在find_package和target_link_libraries中包含了Qt图表。这似乎对qt5有效(在StackOverflow中的其他答案中可以找到(,但对qt6无效。(我已经隐藏了项目源文件(
cmake_minimum_required(VERSION 3.5)
project(projectname VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS_DEBUG "/MDd")
find_package(QT NAMES Qt6 COMPONENTS Widgets Charts REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Xml Widgets REQUIRED)
set(PROJECT_SOURCES
...
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(projectname
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
add_executable(projectname
${PROJECT_SOURCES}
)
endif()
target_link_libraries(projectname PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Xml Qt6::Charts)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(projectname)
endif()
我得到这个错误:
C:Qt6.2.1msvc2019_64libcmakeQt6CoreQt6CoreMacros.cmake:559: error: Target "projectname" links to target "Qt6::Charts" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing? C:/Qt/6.2.1/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:523 (_qt_internal_create_executable) C:/Qt/6.2.1/msvc2019_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:847 (qt6_add_executable) CMakeLists.txt:78 (qt_add_executable)
解决方案(感谢@vre(:
我必须选择6.2.1下的Qt图表,而不是在线安装程序中的Qt下
Qt在线安装程序