插件窝 干货文章 使用CMake构建Linux工业自动化应用程序的配置技巧

使用CMake构建Linux工业自动化应用程序的配置技巧

CMake IndustrialAutoma 配置 tionApp 66    来源:    2025-04-15

CMake构建Linux工业自动化应用程序的配置技巧

1. 基础配置

1.1 最小CMake配置

cmake_minimum_required(VERSION 3.10)
project(IndustrialAutomationApp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

1.2 针对工业应用的优化标志

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    add_compile_options(-O3 -flto -march=native -mtune=native)
    add_link_options(-flto)
endif()

2. 工业通信协议集成

2.1 OPC UA集成

# 查找OPC UA SDK
find_package(open62541 REQUIRED)

# 添加OPC UA组件
target_link_libraries(IndustrialAutomationApp PRIVATE open62541)

2.2 Modbus支持

# 使用libmodbus
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBMODBUS REQUIRED libmodbus)

# 添加Modbus支持
target_include_directories(IndustrialAutomationApp PRIVATE ${LIBMODBUS_INCLUDE_DIRS})
target_link_libraries(IndustrialAutomationApp PRIVATE ${LIBMODBUS_LIBRARIES})

3. 实时性配置

3.1 实时Linux配置

# 检查是否在实时Linux上构建
if(EXISTS "/sys/kernel/realtime")
    add_compile_definitions(LINUX_REALTIME)
    message(STATUS "Building for real-time Linux")

    # 添加实时性链接选项
    find_library(RT_LIB rt)
    target_link_libraries(IndustrialAutomationApp PRIVATE ${RT_LIB} pthread)
endif()

3.2 优先级设置

# 添加实时优先级设置代码
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/src/rt_priority.h.in
    ${CMAKE_CURRENT_BINARY_DIR}/rt_priority.h
)

4. 硬件接口配置

4.1 GPIO访问

# 检查GPIO库
find_library(GPIO_LIB gpiod)
if(GPIO_LIB)
    target_link_libraries(IndustrialAutomationApp PRIVATE ${GPIO_LIB})
    add_compile_definitions(HAS_GPIO_SUPPORT)
endif()

4.2 CAN总线支持

# SocketCAN支持
check_include_files(linux/can.h HAVE_CAN_H)
if(HAVE_CAN_H)
    add_compile_definitions(HAS_CAN_SUPPORT)
endif()

5. 工业安全配置

5.1 加密库集成

# OpenSSL集成
find_package(OpenSSL REQUIRED)
target_link_libraries(IndustrialAutomationApp PRIVATE OpenSSL::SSL OpenSSL::Crypto)

5.2 安全编译选项

# 安全加固编译选项
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(IndustrialAutomationApp PRIVATE
        -fstack-protector-strong
        -D_FORTIFY_SOURCE=2
        -fPIE
    )
    target_link_options(IndustrialAutomationApp PRIVATE -pie -Wl,-z,now,-z,relro)
endif()

6. 工业数据记录

6.1 数据库集成

# SQLite3集成
find_package(SQLite3 REQUIRED)
target_link_libraries(IndustrialAutomationApp PRIVATE SQLite::SQLite3)

6.2 时间序列数据库

# InfluxDB客户端
find_package(influxdb-cxx REQUIRED)
target_link_libraries(IndustrialAutomationApp PRIVATE influxdb-cxx)

7. 部署配置

7.1 安装规则

# 安装主程序
install(TARGETS IndustrialAutomationApp
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# 安装配置文件
install(DIRECTORY config/ DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/industrial_automation)

7.2 系统服务配置

# 安装systemd服务文件
if(UNIX AND NOT APPLE)
    configure_file(
        ${CMAKE_CURRENT_SOURCE_DIR}/industrial-automation.service.in
        ${CMAKE_CURRENT_BINARY_DIR}/industrial-automation.service
    )

    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/industrial-automation.service
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/systemd/system
    )
endif()

8. 测试配置

8.1 单元测试

# Google Test集成
include(GoogleTest)
find_package(GTest REQUIRED)

add_executable(IndustrialAutomationTests tests/main.cpp)
target_link_libraries(IndustrialAutomationTests PRIVATE GTest::GTest GTest::Main IndustrialAutomationApp)

gtest_discover_tests(IndustrialAutomationTests)

8.2 硬件在环测试

# 添加硬件模拟测试
if(ENABLE_HIL_TESTING)
    add_executable(HILTests tests/hil_tests.cpp)
    target_link_libraries(HILTests PRIVATE IndustrialAutomationApp ${RT_LIB})
endif()

9. 交叉编译配置

# 针对工业控制器的交叉编译设置
if(CMAKE_CROSSCOMPILING)
    set(CMAKE_FIND_ROOT_PATH /path/to/sysroot)
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()

10. 高级调试配置

10.1 核心转储配置

# 启用完整核心转储
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_compile_definitions(ENABLE_CORE_DUMP)
    add_compile_options(-g -ggdb3)
endif()

10.2 工业协议调试

# 协议调试选项
option(ENABLE_PROTOCOL_DEBUG "Enable industrial protocol debugging" OFF)
if(ENABLE_PROTOCOL_DEBUG)
    add_compile_definitions(DEBUG_OPCUA=1 DEBUG_MODBUS=1)
endif()

这些配置技巧可以帮助您构建一个功能强大、安全可靠的Linux工业自动化应用程序。根据您的具体需求,可能需要调整或添加特定的配置选项。