0

I would like to include open62541 library to my existing C++ project in Visual Studio using CMake. open62541 itself uses CMake as build tool. Project structure:

MyOPC
│   CMakeLists.txt
│   MyOPC.cpp
│   MyOPC.h
├───.vs
└───open62541
    │   CMakeLists.txt
    ├───arch
    │   │    CMakeLists.txt
    ├───deps
    ├───doc
    ├───examples
    ├───include
    ├───plugins
    ├───src
    ├───tests
    └───tools

I would like to build open62541 togehter with my project so it will produce open62541.h file. How this could be done using CMake?

user1646245
  • 309
  • 4
  • 8

2 Answers2

1

You can simply add the subdirectory of open62541 to your main CMake and before that set the corresponding CMake options. Then also add the open62541 targets to your own target and add the amalgamated source file.

E.g. to enable amalgamation:

set(UA_ENABLE_AMALGAMATION ON CACHE BOOL "" FORCE)
set(UA_LOGLEVEL 300)
add_subdirectory(open62541)

add_dependencies(${PROJECT_NAME} open62541 open62541-amalgamation-source open62541-amalgamation-header)  
set (${PROJECT_NAME}_SRCS ${${PROJECT_NAME}_SRCS} "${PROJECT_BINARY_DIR}/open62541/open62541.c")

Make sure that you add the _SRCS to your own target sources.

Something similar is done here: https://github.com/Pro/open62541-arduino

Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73
1

Thanks @Stefan Profanter for putting me on right direction. This is current working CMakeLists.txt:

# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)

project ("MyOPC")

add_executable (${PROJECT_NAME} "MyOPC.cpp" "MyOPC.h")


# -----------------------------------
# open62541 specific settings - BEGIN
# -----------------------------------
set(UA_ENABLE_AMALGAMATION ON CACHE BOOL "" FORCE)
set(UA_LOGLEVEL 300)
add_subdirectory ("open62541")

set_source_files_properties("${PROJECT_BINARY_DIR}/open62541/open62541.c" PROPERTIES GENERATED TRUE)
set(${PROJECT_NAME}_SRCS ${${PROJECT_NAME}_SRCS} "${PROJECT_BINARY_DIR}/open62541/open62541.c")
include_directories("${PROJECT_BINARY_DIR}/open62541/")
# -----------------------------------
# open62541 specific settings - END
# -----------------------------------

add_dependencies(${PROJECT_NAME} open62541 open62541-amalgamation-source open62541-amalgamation-header) 

target_link_libraries(${PROJECT_NAME} open62541)

Reference in header file MyOPC.h:

#include "open62541.h"
user1646245
  • 309
  • 4
  • 8