使用CMake构建open62541库作为外部库



我想使用CMake将open62541库包含到Visual Studio中现有的C++项目中。open62541本身使用CMake作为构建工具。项目结构:

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

我想用我的项目来构建open62541。使用CMake如何做到这一点?

您只需将open62541的子目录添加到主CMake中,然后设置相应的CMake选项。然后将open62541目标添加到您自己的目标中,并添加合并的源文件。

例如,实现合并:

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")

请确保将_SRCS添加到您自己的目标源中。

这里也做了类似的事情:https://github.com/Pro/open62541-arduino

感谢@Stefan Profanter为我指明了正确的方向。这是当前正在工作的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)

头文件MyOPC.h:中的引用

#include "open62541.h"

相关内容

  • 没有找到相关文章

最新更新