Libraries
Industrial automation projects benefit from modular architecture. Encapsulating custom control algorithms, reusable Function Blocks, signal filters, and communication modules into dedicated libraries promotes code reuse across machines and accelerates build times.
Creating a Library Target
Adapnex provides the adapnex_library() CMake command to define reusable component libraries. It wraps standard CMake add_library() while automatically configuring the include paths, compiler definitions, and linking rules required by the Adapnex Core runtime:
adapnex_library(<name> <sources>...)
Unlike standard CMake libraries where developers must manually link real-time dependencies and threading primitives, adapnex_library() ensures your component compiles consistently across both local simulation and cross-compiled hardware targets.
Exporting Include Paths
To make a library’s header files accessible to other targets, specify its public include directory using standard CMake target_include_directories() with the PUBLIC specifier:
adapnex_library(
motion_control
src/ramp_generator.cpp
)
target_include_directories(
motion_control
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
Any executable or test suite that links motion_control will inherit this include directory automatically.
Linking Libraries to Applications
To consume a library in an application executable, declare the executable target with adapnex_executable() and link the library using target_link_libraries():
adapnex_executable(
conveyor_app
main.cpp
)
target_link_libraries(
conveyor_app
PRIVATE
motion_control
)
The application now has access to all public headers, tasks, and Function Blocks exported by motion_control.
Testing Library Components
Because libraries created with adapnex_library() depend only on portable Adapnex interfaces, you can test them directly with Google Test on your development machine using adapnex_tests():
# Define a test runner executable
adapnex_tests(
motion_tests
tests/ramp_generator_test.cpp
)
# Link the library to the test suite
target_link_libraries(
motion_tests
PRIVATE
motion_control
)
You can run the tests directly from the command line with adapnex test:
adapnex test
In Visual Studio Code, the Test Explorer discovers these tests automatically, allowing you to run or debug individual test cases with a single click.
CMake Configuration Example
The following CMakeLists.txt demonstrates how to define a reusable library, link it to an application executable, and attach a unit test suite in a single concise file:
cmake_minimum_required(VERSION 3.29)
project(packaging_machine)
# 1. Define the reusable motion control library
adapnex_library(
motion_control
src/ramp_generator.cpp
)
target_include_directories(
motion_control
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
# 2. Define the main application executable and link the library
adapnex_executable(
line_controller
src/main.cpp
)
target_link_libraries(
line_controller
PRIVATE
motion_control
)
# 3. Define the unit test runner and link the library
enable_testing()
adapnex_tests(
motion_tests
tests/ramp_generator_test.cpp
)
target_link_libraries(
motion_tests
PRIVATE
motion_control
)