7.1. Hello World

Important

|fa-windows| This tutorial will only work with eCAL 5.7.3 and upwards. It will not work with older versions that were published as .msi installer (it missed some libraries).

Please switch to Ubuntu, if you are using an old eCAL Version.

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:

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:

  • |fa-file-alt| CMakeLists.txt:

    Note

    What is happening here?

    Line 3 creates a project “hello_world_snd”. This will also be the name of the executable (line 14).

    Line 5-6 set the C++ standard to C++14

    Line 8 tells CMake to find the eCAL installation. Line 16-19 will link the executable against it.

    Line 10-12 create a list of all our source files, which currently only contains main.cpp. We add that source file for compiling our executable in line 14.

    Line 16-19 link our executable against the eCAL core library.

  • |fa-file-alt| main.cpp:

    Note

    What is happening here?

    Line 1 includes the basic eCAL header. As we want to publish raw strings, line 2 includes the eCAL String-Publisher. eCAL Supports multiple message formats.

    Line 10 initialized eCAL. You always have to initialize eCAL before using its API. The name of our eCAL Process will be “Hello World Publisher”. This name will be visible in the eCAL Monitor, once the Process is running.

    Line 13 creates an eCAL Publisher. An eCAL Process can create multiple publishers (and multiple subscribers). The topic we are publishing on will be “hello_world_topic”.

    The while loop from line 20 will cause an infinite publish-loop. eCAL supports a stop-signal; when an eCAL Process is stopped, eCAL::Ok() will return false.

    Line 25 will publish our message and send it to other eCAL Processes that have subscribed on the topic.

    Line 32 de-initializes eCAL. You should always do that before your application exits.

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

  • |fa-windows| Windows:

    mkdir _build
    cd _build
    cmake .. -A x64
    cmake --build . --parallel
    
  • |fa-ubuntu| Ubuntu:

    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:

  • |fa-file-alt| CMakeLists.txt:

    Note

    What is happening here?

    Line 3 creates a project “hello_world_rec”. This is the only difference to the hello_world_snd Project.

  • |fa-file-alt| main.cpp:

    Note

    What is happening here?

    Line 8-11 Is the receive callback. This method will be called whenever a new message arrives.

    Line 19 creates an eCAL subscriber that listens to the “hello_world_topic”.

    Line 22 adds the receive callback from above to the subscriber, so it can be called.

    Important

    eCAL Receive callbacks run in the subscriber’s receive thread. While the callback is running, the subscriber cannot receive new data. So, if your callback needs really long to compute, you may have to decouple your computations to not lose messages.

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

  • |fa-windows| Windows:

    mkdir _build
    cd _build
    cmake .. -A x64
    cmake --build . --parallel
    
  • |fa-ubuntu| Ubuntu:

    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

|fa-folder-open|
├─ |fa-folder-open| hello_world_snd
│  ├─ |fa-file-alt| CMakeLists.txt
│  └─ |fa-file-alt| main.cpp
│
└─ |fa-folder-open| hello_world_rec
   ├─ |fa-file-alt| CMakeLists.txt
   └─ |fa-file-alt| main.cpp