【问题标题】:open62541: Linker Tool Error for referenced functionsopen62541:引用函数的链接器工具错误
【发布时间】:2020-11-14 04:50:15
【问题描述】:

我正在尝试让 open62541 在我的 Windows 10 机器上运行,但即使使用此 post,我仍然在苦苦挣扎。

目标

我想执行一个具有所有相关功能(PLC 变量上的 CRUD 等)的 cpp OPC_UA 客户端。

当前状态

我已经按照官方docs和这个post构建了open62541项目:

cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -UA_NAMESPACE_ZERO=FULL ..

之后我运行 ALL_BUILD 和 INSTALL 没有任何问题(如果我以管理员身份运行 VisualStudio 16 2019)。因此,我在Program files (x86) 下有 open62541 文件夹,其中包含 .h、.dll 和 .lib 文件:


下一步是创建包含客户端代码的 CMake 项目。我使用 CMake GUI 链接 open62541 文件/文件夹,但我也必须在我的 CMakeSetting.json 中这样做:

Test.cpp

#include "open62541.h"
#include <iostream>

int main()
{
    UA_Client* client = UA_Client_new();
    UA_Client_delete(client);
    std::cout << "Hello CMake." << std::endl;
    return 0;
}

CMakeList.txt

cmake_minimum_required (VERSION 3.8)

project ("Test")
add_subdirectory ("Test")

# Find the generated/amalgamated header file
find_path(OPEN62541_INCLUDE_DIR open62541.h)

# Find the generated .lib file
find_library(OPEN62541_LIBRARY open62541)

# Find open62541 with dependencies (Full NS0)
find_package(open62541 REQUIRED COMPONENTS FullNamespace)

# Include open62541 include folder 
include_directories(${OPEN62541_INCLUDE_DIR})

# Set open62541 libary 
set(open62541_LIBRARIES ${open62541_LIBRARIES} ${OPEN62541_LIBRARY})

# Create main.exe
add_executable(main "Test/Test.cpp")

# Link open62541 to main. 
target_link_libraries(main ${open62541_LIBRARIES})

CMakeSettings.json

{
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "OPEN62541_LIBRARY",
          "value": "C:/Program Files (x86)/open62541/lib/open62541.lib",
          "type": "FILEPATH"
        },
        {
          "name": "OPEN62541_INCLUDE_DIR",
          "value": "C:/Program Files (x86)/open62541/include",
          "type": "PATH"
        }
      ]
    }
  ]
}

问题

一旦我构建项目或执行main.exe,我会为每个引用的 OPC UA 对象实例收到 LNK2019 错误:

LNK2019 unresolved external symbol __imp_UA_Client_delete referenced in function main   

我也尝试使用 open62541 项目中的构建示例,但出现相同的错误。

【问题讨论】:

    标签: c++ visual-studio cmake linker open62541


    【解决方案1】:

    installation documentation 描述了使用 导入的 CMake 目标将 open62541 链接到您的 CMake 目标:

    find_package(open62541 REQUIRED COMPONENTS Events FullNamespace)
    add_executable(main main.cpp)
    target_link_libraries(main open62541::open62541)
    

    通过使用导入的目标,您的 CMake 代码中的以下命令将变得不必要:

    • find_path
    • find_library
    • include_directories

    另外,如果你还没有这样做,你需要告诉 CMake 在哪里 在你的系统上找到 open62541。您可以通过将生成的open62541Config.cmake 文件的路径附加到CMAKE_PREFIX_PATH 变量来执行此操作,如here 所述。


    此外,尚不清楚您使用哪些选项运行 cmake,但安装文档建议使用这些选项运行:

    cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUA_NAMESPACE_ZERO=FULL ..
    

    【讨论】:

    • 感谢您的回答。我相应地编辑了我的 CMakeLists.txt 并使用您提供的选项再次重建了 open62541。仍然是同样的问题:当我设置了 Events 标志时,我也得到了一个错误。
    • 我将此添加到我的 open62541Config.cmake:list(APPEND CMAKE_PREFIX_PATH ${PACKAGE_PREFIX_DIR}),其中包前缀目录进入我的 open62541 文件夹,如第一张图片所示
    • @StephanT。 list(APPEND ...) 行应在调用find_package() 之前添加到您自己的CMakeLists.txt 文件中,您不应修改open62541Config.cmake 文件。
    • 啊,你说“你可以通过将路径附加到生成的 open62541Config.cmake 文件来做到这一点”,所以我以为你的意思是我将这一行添加到 cmake 文件中
    • @StephanT。对不起,我的措辞不好。这意味着您需要将 open62541Config.cmake 文件所在的路径插入到 CMAKE_PREFIX_PATH 变量中。希望这是有道理的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2023-04-06
    • 2012-06-13
    相关资源
    最近更新 更多