7.4. CMake Integration#
In this chapter we will show how to setup a simply CMake project using eCAL. We will show the two most common use cases to generate eCAL processes. With this knowledge you will be able to create your own CMake project and transfer the knowledge to other use cases.
7.4.1. Basic string entity integration#
For a basic integration, you will need to call find_package(eCAL REQUIRED)
in addition to your normal project.
Furthermore, you link the eCAL::string_core
in order to be able to create string publishers and subscribers.
The CMakeLists.txt looks as follows:
cmake_minimum_required(VERSION 3.16)
# This CMakeLists.txt is used to build a protobuf example for eCAL.
# So we name this project, which is also the name of the executable, accordingly.
project(hello_world_send)
# We set the C++ standard to C++14, which is currently required by eCAL.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Tell CMake to find the eCAL installation.
find_package(eCAL REQUIRED)
# Set a list all source files for the project.
# In our case it is only one file, but can be simply extended to multiple files.
set(source_files
hello_world_send.cpp
)
# Now we add the source file to the executable to be generated.
# The name of the executable is the same as the project name.
add_executable(${PROJECT_NAME} ${source_files})
# Finally we need to link the necessary libararies to the executable.
# Which target you link to depends on the type of the message you are using.
# In our case we use the string message, so we link to the eCAL string core library.
target_link_libraries(${PROJECT_NAME}
PRIVATE
eCAL::string_core
)
7.4.2. Protobuf entity integration#
In addtition to the previous example, you will need to call find_package(Protobuf REQUIRED)
.
Furthermore you want to handle the generation of the protobuf headers for your language.
cmake_minimum_required(VERSION 3.16)
# This CMakeLists.txt is used to build a protobuf example for eCAL.
# So we name this project, which is also the name of the executable, accordingly.
project(ecal_hello_world_send_protobuf)
# We set the C++ standard to C++14, which is currently required by eCAL.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Tell CMake to find the eCAL installation and the protobuf library.
find_package(eCAL REQUIRED)
find_package(Protobuf REQUIRED)
# Set a list all source files for the project.
# In our case it is only one file, but can be simply extended to multiple files.
set(source_files
hello_world_send_protobuf.cpp
)
# We set a list of protobuf files here. The path is relative to the CMakeLists.txt file.
# This CMakeLists.txt assumes that the protobuf files are located in the ../proto_messages/ directory.
set(protobuf_files
${CMAKE_CURRENT_SOURCE_DIR}/../proto_messages/hello_world.proto
)
# Now we add the source file to the executable to be generated.
# The name of the executable is the same as the project name.
add_executable(${PROJECT_NAME} ${source_files})
# We need to tell CMake to generate the protobuf files. This is done by the PROTOBUF_TARGET_CPP function.
# This compiles the .proto file to a C++ header, in our case "hello_world.pb.h".
# Note that the PROTOBUF_TARGET_CPP function is a eCAL convenience function.
# You can also use the protobuf_generate_cpp function from the protobuf package directly if you are already familiar with it.
PROTOBUF_TARGET_CPP(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/../proto_messages/ ${protobuf_files})
# Finally we need to link the necessary libararies to the executable.
# Which target you link to depends on the type of the message you are using.
# In our case we use the protobuf message, so we link to the eCAL protobuf core library.
target_link_libraries(${PROJECT_NAME}
PRIVATE
eCAL::protobuf_core
)
Tip
The PROTOBUF_TARGET_CPP
function is a convenience function from eCAL.
If you have already worked with Protobuf and CMake, you may be more familiar with the following code, which basically does the same thing:
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${protobuf_files})
add_executable(${PROJECT_NAME} ${source_files} ${PROTO_SRCS} ${PROTO_HDRS})
7.4.3. Common CMake targets#
It follows a list of common eCAL CMake targets.
CMake target |
Description |
---|---|
eCAL::core |
C++ core functionality |
eCAL::string_core |
C++ std::string publisher/subscriber extension (implicitly links eCAL::core) |
eCAL::protobuf_core |
C++ protobuf publisher/subscriber/client/server extension (implicitly links eCAL::core) |
eCAL::capnproto_core |
C++ capnproto publisher/subscriber extension (implicitly links eCAL::core) |
eCAL::flatbuffers_core |
C++ flatbuffers publisher/subscriber extension (implicitly links eCAL::core) |
eCAL::measurement |
read/write binary eCAL measurements |
eCAL::string_measurement |
read/write eCAL measurements with string (implicitly links eCAL::measurement) |
eCAL::protobuf_measurement |
read/write eCAL measurements with protobuf (implicitly links eCAL::measurement) |
eCAL::capnproto_measurement |
read/write eCAL measurements with capnproto (implicitly links eCAL::measurement) |
eCAL::flatbuffers_measurement |
read/write eCAL measurements with flatbuffers (implicitly links eCAL::measurement) |
eCAL::core_c |
C core functionality |