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:
- Visual Studio (https://visualstudio.microsoft.com/)
- CMake (https://cmake.org/download/)
-
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 1cmake_minimum_required(VERSION 3.0)2set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)34project(hello_world_snd)56set(CMAKE_CXX_STANDARD 14)7set(CMAKE_CXX_STANDARD_REQUIRED ON)89find_package(eCAL REQUIRED)1011set(source_files12main.cpp13)1415add_executable(${PROJECT_NAME} ${source_files})1617target_link_libraries(${PROJECT_NAME}18eCAL::core19) -
main.cpp 1#include <ecal/ecal.h>2#include <ecal/msg/string/publisher.h>34#include <iostream>5#include <thread>67int main(int argc, char** argv)8{9// Initialize eCAL. The name of our Process will be "Hello World Publisher"10eCAL::Initialize(argc, argv, "Hello World Publisher");1112// Create a String Publisher that publishes on the topic "hello_world_topic"13eCAL::string::CPublisher<std::string> publisher("hello_world_topic");1415// Create a counter, so something changes in our message16int counter = 0;1718// Infinite loop (using eCAL::Ok() will enable us to gracefully shutdown the19// Process from another application)20while (eCAL::Ok())21{22// Create a message with a counter an publish it to the topic23std::string message = "Hello World " + std::to_string(++counter);24std::cout << "Sending message: " << message << std::endl;25publisher.Send(message);2627// Sleep 500 ms28std::this_thread::sleep_for(std::chrono::milliseconds(500));29}3031// finalize eCAL API32eCAL::Finalize();33}
Now that you have the source code ready, create a _build
directory and build the code!
-
Windows:
Terminal window mkdir \_buildcd \_buildcmake .. -A x64cmake --build . --parallel -
Ubuntu:
Terminal window mkdir \_buildcd \_buildcmake ..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”.
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 1cmake_minimum_required(VERSION 3.0)2set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)34project(hello_world_rec)56set(CMAKE_CXX_STANDARD 14)7set(CMAKE_CXX_STANDARD_REQUIRED ON)89find_package(eCAL REQUIRED)1011set(source_files12main.cpp13)1415add_executable(${PROJECT_NAME} ${source_files})1617target_link_libraries(${PROJECT_NAME}18eCAL::core19) -
main.cpp 1#include <ecal/ecal.h>2#include <ecal/msg/string/subscriber.h>34#include <iostream>5#include <thread>67// Callback for receiving messages8void HelloWorldCallback(const std::string& message)9{10std::cout << "Received Message: " << message << std::endl;11}1213int main(int argc, char** argv)14{15// Initialize eCAL16eCAL::Initialize(argc, argv, "Hello World Subscriber");1718// Create a subscriber that listenes on the "hello_world_topic"19eCAL::string::CSubscriber<std::string> subscriber("hello_world_topic");2021// Set the Callback22subscriber.AddReceiveCallback(std::bind(&HelloWorldCallback, std::placeholders::_2));2324// Just don't exit25while (eCAL::Ok())26{27std::this_thread::sleep_for(std::chrono::milliseconds(500));28}2930// finalize eCAL API31eCAL::Finalize();32}
Now that you have the source code ready, create a _build
directory and build the code!
-
Windows:
Terminal window mkdir \_buildcd \_buildcmake .. -A x64cmake --build . --parallel -
Ubuntu:
Terminal window mkdir \_buildcd \_buildcmake ..make
When you now execute hello_world_snd
and hello_world_rec
, the receiver application will receive the messages sent by the sender.
In the next chapter you will learn how to properly structure your messages with protobuf!
7.1.4 Files
Directory
Directoryhello_world_snd
Directoryhello_world_rec