Skip to content

7.1 Hello World

After you have learned a lot about the pre-compiled applications that come with eCAL, let’s create our own! In the good habit of every tutorial, we will write a Hello World Application, that sends the string “Hello World” to an eCAL topic.

eCAL uses CMake as a cross-platform build toolchain. We will explain the CMake commands needed for the tutorial, but not extensively dive into CMake.

7.1.1 Dependencies

First, you have to install some more development dependencies:

  • Windows:

  • Ubuntu:

    Terminal window
    sudo apt install cmake g++ libprotobuf-dev protobuf-compiler

7.1.2 Hello World Publisher

Somewhere on your hard drive create an empty directory and create a file CMakeLists.txt and main.cpp with the following content:

  • CMakeLists.txt
    1
    cmake_minimum_required(VERSION 3.0)
    2
    set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
    3
    4
    project(hello_world_snd)
    5
    6
    set(CMAKE_CXX_STANDARD 14)
    7
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    8
    9
    find_package(eCAL REQUIRED)
    10
    11
    set(source_files
    12
    main.cpp
    13
    )
    14
    15
    add_executable(${PROJECT_NAME} ${source_files})
    16
    17
    target_link_libraries(${PROJECT_NAME}
    18
    eCAL::core
    19
    )
  • main.cpp
    1
    #include <ecal/ecal.h>
    2
    #include <ecal/msg/string/publisher.h>
    3
    4
    #include <iostream>
    5
    #include <thread>
    6
    7
    int main(int argc, char** argv)
    8
    {
    9
    // Initialize eCAL. The name of our Process will be "Hello World Publisher"
    10
    eCAL::Initialize(argc, argv, "Hello World Publisher");
    11
    12
    // Create a String Publisher that publishes on the topic "hello_world_topic"
    13
    eCAL::string::CPublisher<std::string> publisher("hello_world_topic");
    14
    15
    // Create a counter, so something changes in our message
    16
    int counter = 0;
    17
    18
    // Infinite loop (using eCAL::Ok() will enable us to gracefully shutdown the
    19
    // Process from another application)
    20
    while (eCAL::Ok())
    21
    {
    22
    // Create a message with a counter an publish it to the topic
    23
    std::string message = "Hello World " + std::to_string(++counter);
    24
    std::cout << "Sending message: " << message << std::endl;
    25
    publisher.Send(message);
    26
    27
    // Sleep 500 ms
    28
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    29
    }
    30
    31
    // finalize eCAL API
    32
    eCAL::Finalize();
    33
    }

Now that you have the source code ready, create a _build directory and build the code!

  • Windows:

    Terminal window
    mkdir \_build
    cd \_build
    cmake .. -A x64
    cmake --build . --parallel
  • Ubuntu:

    Terminal window
    mkdir \_build
    cd \_build
    cmake ..
    make

Now execute the hello_world_snd (.exe) and take a look at the eCAL Monitor! You will see the “Hello World Publisher” process and the “hello_world_topic”.

eCAL Monitor Hello World

7.1.3 Hello World Subscriber

Again, create a new directory somewhere and add create the CMakeLists.txt and main.cpp with the following content:

  • CMakeLists.txt
    1
    cmake_minimum_required(VERSION 3.0)
    2
    set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
    3
    4
    project(hello_world_rec)
    5
    6
    set(CMAKE_CXX_STANDARD 14)
    7
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    8
    9
    find_package(eCAL REQUIRED)
    10
    11
    set(source_files
    12
    main.cpp
    13
    )
    14
    15
    add_executable(${PROJECT_NAME} ${source_files})
    16
    17
    target_link_libraries(${PROJECT_NAME}
    18
    eCAL::core
    19
    )
  • main.cpp
    1
    #include <ecal/ecal.h>
    2
    #include <ecal/msg/string/subscriber.h>
    3
    4
    #include <iostream>
    5
    #include <thread>
    6
    7
    // Callback for receiving messages
    8
    void HelloWorldCallback(const std::string& message)
    9
    {
    10
    std::cout << "Received Message: " << message << std::endl;
    11
    }
    12
    13
    int main(int argc, char** argv)
    14
    {
    15
    // Initialize eCAL
    16
    eCAL::Initialize(argc, argv, "Hello World Subscriber");
    17
    18
    // Create a subscriber that listenes on the "hello_world_topic"
    19
    eCAL::string::CSubscriber<std::string> subscriber("hello_world_topic");
    20
    21
    // Set the Callback
    22
    subscriber.AddReceiveCallback(std::bind(&HelloWorldCallback, std::placeholders::_2));
    23
    24
    // Just don't exit
    25
    while (eCAL::Ok())
    26
    {
    27
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    28
    }
    29
    30
    // finalize eCAL API
    31
    eCAL::Finalize();
    32
    }

Now that you have the source code ready, create a _build directory and build the code!

  • Windows:

    Terminal window
    mkdir \_build
    cd \_build
    cmake .. -A x64
    cmake --build . --parallel
  • Ubuntu:

    Terminal window
    mkdir \_build
    cd \_build
    cmake ..
    make

When you now execute hello_world_snd and hello_world_rec, the receiver application will receive the messages sent by the sender.

eCAL Hello World sender and receiver

In the next chapter you will learn how to properly structure your messages with protobuf!

7.1.4 Files