我试图让 open62541 在我的 Windows 10 机器上工作,但即使有这篇文章,我仍然在苦苦挣扎。
目标
我想执行一个具有所有相关功能(PLC 变量上的 CRUD 等)的 cpp OPC_UA 客户端。
当前状态
我已经根据官方文档和这篇文章构建了 open62541 项目:
cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -UA_NAMESPACE_ZERO=FULL ..
之后,我运行 ALL_BUILD 和 INSTALL 没有任何问题(如果我以管理员身份运行 VisualStudio 16 2019)。因此,我在 open62541 文件夹下Program files (x86)
有 .h、.dll 和 .lib 文件:
下一步是创建包含客户端代码的 CMake 项目。我使用 CMake GUI 链接 open62541 文件/文件夹,但我也必须在我的 CMakeSetting.json 中这样做:
测试.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 项目中的构建示例进行了尝试,但出现了相同的错误。