open62541: Linker Tool Error for referenced functions

721 views Asked by At

I am trying to get open62541 to work on my Windows 10 machine, but even with this post, I am still struggling.

Goal

I want to execute a cpp OPC_UA Client with all associated functionalities (CRUD on PLC Variables and so on).

Current status

I already built the open62541 project according to the official docs and this post:

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

After that I ran ALL_BUILD and INSTALL without any problems (if I run VisualStudio 16 2019 as an Admin). Therefore, I have the open62541 folder under Program files (x86) with the .h, .dll and .lib files:

enter image description here


The next step is to create the CMake project containing the client code. I used the CMake GUI to link the open62541 files/folders, but I had to do that in my CMakeSetting.json too:

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"
        }
      ]
    }
  ]
}

Problem

Once I build the project or execute main.exe, I get LNK2019 errors for every instance of referenced OPC UA objects:

LNK2019 unresolved external symbol __imp_UA_Client_delete referenced in function main   

I tried this using the build examples in the open62541 project too, but with the same errors.

1

There are 1 answers

6
Kevin On

The installation documentation describes using an imported CMake target to link open62541 to your CMake targets:

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

By using the imported target, the following commands in your CMake code become unnecessary:

  • find_path
  • find_library
  • include_directories

In addition, if you haven't done so already, you need to tell CMake where to find open62541 on your system. You can do this by appending the path to the generated open62541Config.cmake file to the CMAKE_PREFIX_PATH variable, as described here.


Also, it isn't clear what options you ran cmake with, but the installation docs suggest running with these options:

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