Qt OPC UA 插件始终在调试配置中链接

问题描述 投票:0回答:1

我正在使用 CMake 构建一个 Qt 项目,尝试将其与 Qt OPC UA 插件链接。

问题是,无论我选择哪种构建配置,编译后的可执行文件总是会链接到

Qt6OpcUad.dll
(因此,调试版本)而不是预期的
QtOpcUa.dll
,无论我选择哪种构建配置。手动运行 exe 时的错误对话框和 DependencyWalker 都证实了这一点。

我正在链接教程中给出的插件:

find_package(Qt6 REQUIRED COMPONENTS Core Gui OpcUa Widgets)
target_link_libraries(opcuaviewer PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::OpcUa
    Qt6::Widgets
)

该插件是按照here所述进行编译的,并且在调试模式下运行时似乎工作正常。我可以使用 Qt Creator 构建并运行 Qt OPC UA 示例应用程序“Opc UA Viewer”。

QtOpcUa.dll
Qt6OpcUad.dll
都存在于
C:\Qt\6.7.1\msvc2019_64\bin
中。

当尝试设置

MAP_IMPORTED_CONFIG
变量时,如herehere所述, CMake 给出这些错误消息:

[cmake] CMake Error in CMakeLists.txt:
[cmake]   IMPORTED_IMPLIB not set for imported target "Qt6::OpcUa" configuration
[cmake]   "Release".
[cmake] 
[cmake] CMake Error in CMakeLists.txt:
[cmake]   IMPORTED_LOCATION not set for imported target "Qt6::OpcUa" configuration
[cmake]   "Release".
[cmake] 
[cmake] CMake Error in CMakeLists.txt:
[cmake]   IMPORTED_IMPLIB not set for imported target "Qt6::OpcUa" configuration
[cmake]   "Release".
[cmake] 
[cmake] -- Generating done (0.0s)
[cmake] CMake Generate step failed.  Build files cannot be regenerated correctly.

如果我在

find_package
之前设置CMake值,这对于两者来说是相同的:

set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE Release)
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpcUa Widgets)

或者尝试在链接后设置目标属性:

target_link_libraries(opcuaviewer PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::OpcUa
    Qt6::Widgets
)
set_target_properties(Qt6::OpcUa PROPERTIES MAP_IMPORTED_CONFIG_RELEASE Release)

不过,为

Qt6::Core
设置相同的属性是可行的。

这是使用Qt6.7.1,Windows 10,并使用MSVC2019 64位编译的。


更新:

按照@Tsyvarev 的建议,我将以下内容添加到 CMake 文件中:

set_target_properties(Qt6::OpcUa PROPERTIES IMPORTED_IMPLIB_RELEASE "C:/Qt/6.7.1/msvc2019_64/lib/Qt6OpcUa.lib")
set_target_properties(Qt6::OpcUa PROPERTIES IMPORTED_LOCATION_RELEASE "C:/Qt/6.7.1/msvc2019_64/lib/Qt6OpcUa.dll")

set_target_properties(Qt6::OpcUa PROPERTIES MAP_IMPORTED_CONFIG_DEBUG Release)
set_target_properties(Qt6::OpcUa PROPERTIES MAP_IMPORTED_CONFIG_RELEASE Release)

构建良好,但仍然是调试版本。 然后我删除了该插件的本地调试版本。 CMake 现在将报告这一点:

[cmake] CMake Error at C:/Qt/6.7.1/msvc2019_64/lib/cmake/Qt6OpcUa/Qt6OpcUaTargets.cmake:126 (message):
[cmake]   The imported target "Qt6::OpcUa" references the file
[cmake] 
[cmake]      "C:/Qt/6.7.1/msvc2019_64/lib/Qt6OpcUad.lib"
[cmake] 
[cmake]   but this file does not exist.  Possible reasons include:
[cmake] 
[cmake]   * The file was deleted, renamed, or moved to another location.
[cmake] 
[cmake]   * An install or uninstall procedure did not complete successfully.
[cmake] 
[cmake]   * The installation package was faulty and contained
[cmake] 
[cmake]      "C:/Qt/6.7.1/msvc2019_64/lib/cmake/Qt6OpcUa/Qt6OpcUaTargets.cmake"
[cmake] 
[cmake]   but not all the files it references.

该文件确实不再存在。但一开始就不应该引用它。


这是我的完整 CMakeLists.txt。注释中的所有命令都会导致所描述的错误消息:

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

message("CMAKE_AUTOUIC is '" ${CMAKE_AUTOUIC} "'")
message("CMAKE_AUTOMOC is '" ${CMAKE_AUTOMOC} "'")
message("CMAKE_AUTORCC is '" ${CMAKE_AUTORCC} "'")
message("CMAKE_BUILD_TYPE is '" ${CMAKE_BUILD_TYPE} "'")

cmake_minimum_required(VERSION 3.16)
project(opcuaviewer LANGUAGES CXX)

if(NOT DEFINED INSTALL_EXAMPLESDIR)
    set(INSTALL_EXAMPLESDIR "examples")
endif()

set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opcua/opcuaviewer")

# set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE Release)
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpcUa Widgets)


qt_standard_project_setup()

qt_add_executable(opcuaviewer
    certificatedialog.cpp certificatedialog.h certificatedialog.ui
    main.cpp
    mainwindow.cpp mainwindow.h mainwindow.ui
    opcuamodel.cpp opcuamodel.h
    treeitem.cpp treeitem.h
)

qt_add_resources(opcuaviewer "pki"
    PREFIX /
    FILES
        pki/own/certs/opcuaviewer.der
        pki/own/private/opcuaviewer.pem
        pki/trusted/certs/3d8ec65c47524d6ad67bed912c19a895.der
        pki/trusted/certs/ca.der
        pki/trusted/certs/open62541-testserver.der
        pki/trusted/crl/ca.crl.pem
)
#set_target_properties(Qt6::OpcUa PROPERTIES IMPORTED_LOCATION_RELEASE "C:/Qt/6.7.1/msvc2019_64/lib/Qt6OpcUa.lib")

set_target_properties(opcuaviewer PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)
target_link_libraries(opcuaviewer PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::OpcUa
    Qt6::Widgets
)
# set_target_properties(Qt6::Core PROPERTIES MAP_IMPORTED_CONFIG_DEBUG Release)


install(TARGETS opcuaviewer
    RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
    BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
    LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

更新二: 我安装插件的方式似乎有问题,因为缺少

C:\Qt\6.7.1\msvc2019_64\lib\cmake\Qt6OpcUa\Qt6OpcUaTargets-release.cmake
。这是我使用的完整批处理脚本:

set msvcSetEnvScript="C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat"
set qtVersion=6.7.1
set qtSetEnvScript="C:\Qt\%qtVersion%\msvc2019_64\bin\qtenv2.bat"
set buildDir=

pushd "C:\"
call %qtSetEnvScript%
call %msvcSetEnvScript%
popd

git clone https://code.qt.io/qt/qtopcua.git 
cd qtopcua 
git checkout %qtVersion% 

REM Build Release.
mkdir build 
cd build 
call qt-cmake -G "NMake Makefiles" .. -DQT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release
nmake 
nmake install 

REM Build Debug.
cd ..
mkdir build_debug
cd build_debug
call qt-cmake -G "NMake Makefiles" .. -DQT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Debug
nmake 
nmake install 

popd

qt cmake qtplugin
1个回答
0
投票

只是想结束这个循环。

我不确定到底在哪一点出了问题,但原因显然是插件 CMake 脚本的安装例程中的错误。

通过卸载所有内容,然后仅安装发布版本,我能够使我的项目与插件的发布版本链接。这样做的缺点是,现在对于两种构建配置(发布和调试),我的项目将仅链接到发布。目前这对我来说已经足够了。

再次感谢@Tsyvarev 帮助我调试这个。不幸的是,我无法通过提到的任何 CMake 设置来修复插件行为。

© www.soinside.com 2019 - 2024. All rights reserved.