我正在尝试使用
Qt6
安装 vcpkg
,并在 Visual Studio 2019 CMake project
中使用它。我尝试过链接其他库(例如:fmt
)并且它有效。但是,当我在混合中添加 Qt
时,构建成功,但运行可执行文件失败:
弹出窗口显示:
Program ... QtCored.dll
...
This application failed to start because no Qt platform could be initialized
...
控制台显示
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in "C:\vcpkg\installed\x64-windows\Qt6\plugins\platforms"
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
环境变量:我将
VCPKG_ROOT
设置为C:\vcpkg
,QT_PLUGIN_PATH
设置为C:\vcpkg\installed\x64-windows\Qt6\plugins\platforms
,QT_QPA_PLATFORM_PLUGIN_PATH
设置为C:\vcpkg\installed\x64-windows\Qt6\plugins\platforms
,将C:\vcpkg\installed\x64-windows\Qt6
和C:\vcpkg
添加到路径中。
我尝试将exe放在一个单独的文件夹中,并在其上运行windeployqt.exe,但它也失败了......
windeployqt.exe C:\Users\broland\Desktop\NewFolder\HelloWorld.exe
Warning: Translations will not be available due to the following error.
Cannot open C:/vcpkg/installed/x64-windows/translations/Qt6/catalogs.json
Unable to find dependent libraries of C:\vcpkg\installed\x64-windows\bin\Qt6Widgetsd.dll :Cannot open 'C:/vcpkg/installed/x64-windows/bin/Qt6Widgetsd.dll': The system cannot find the file specified.
我的CMakeLists.txt:
cmake_minimum_required (VERSION 3.8)
set(CMAKE_AUTOMOC ON) # https://stackoverflow.com/questions/14170770/unresolved-external-symbol-public-virtual-struct-qmetaobject-const-thiscal
set(CMAKE_INCLUDE_CURRENT_DIR ON) # so that mainwindow.h and mainwindow.cpp is seen
project (HelloWorld)
find_package(fmt CONFIG REQUIRED)
find_package(Qt6Core REQUIRED)
find_package(Qt6Gui CONFIG REQUIRED)
find_package(Qt6Widgets REQUIRED)
add_executable (HelloWorld "helloworld.cpp" "helloworld.h" "mainwindow.cpp")
#add_executable (HelloWorld "mainwindow.cpp" "mainwindow.h" "mainwindow.cpp")
target_link_libraries(HelloWorld PRIVATE fmt::fmt)
target_link_libraries(HelloWorld PRIVATE Qt::Gui)
target_link_libraries(HelloWorld PRIVATE Qt::Core)
target_link_libraries(HelloWorld PRIVATE Qt::Widgets)
已经尝试了好几天了。有什么建议吗?
因此,我没有使用 vcpkg,而是通过在线安装程序安装了 Qt。
对于其他陷入困境的人,我将把它留在这里。我遵循了 this 和 this 教程的部分内容。
由于我使用Visual Studio,所以我下载了
MSVC 2019 64-bit
版本。我将环境变量 QTDIR
设置为等于 msvc2019_64
文件夹所在的路径。我添加到 Path
环境中。变种%QTDIR%\lib
和%QTDIR%\bin
。为了检查我是否可以链接其他vcpkg
安装的库,我得到了OpenCV
(vcpkg install opencv
)。似乎有效。 INCLUDE
中的路径是我拥有各自文件的位置,ip、ua和var是我的项目的文件夹,其中包含源文件和头文件。
我的CMakeLists.txt:
cmake_minimum_required (VERSION 3.8)
project (OpenCBDependencyTest)
#some magic
set(CMAKE_AUTOMOC ON)
#set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE(C:/vcpkg/scripts/buildsystems/vcpkg.cmake) # so that vcpkg installed packages are seen
# find packages
find_package(Qt6Core REQUIRED)
find_package(Qt6Gui REQUIRED)
find_package(Qt6Widgets REQUIRED)
find_package(OpenCV REQUIRED)
# include files
set(IP_DIR "${CMAKE_SOURCE_DIR}/ip")
set(UA_DIR "${CMAKE_SOURCE_DIR}/ua")
set(VAR_DIR "${CMAKE_SOURCE_DIR}/var")
include_directories(${IP_DIR})
include_directories(${UA_DIR})
include_directories(${VAR_DIR})
file(GLOB_RECURSE SOURCES
"${IP_DIR}/*.cpp"
"${IP_DIR}/*.h"
"${UA_DIR}/*.cpp"
"${UA_DIR}/*.h"
"${VAR_DIR}/*.cpp"
"${VAR_DIR}/*.h"
"Main.cpp"
)
add_executable (OpenCBDependencyTest ${SOURCES})
# link libraries
target_link_libraries(OpenCBDependencyTest Qt::Core)
target_link_libraries(OpenCBDependencyTest Qt::Gui)
target_link_libraries(OpenCBDependencyTest Qt::Widgets)
target_link_libraries(OpenCBDependencyTest ${OpenCV_LIBS})