The classic hello world


In this short tutorial we learn how to use CMake. We compile a simple test program that links all YARP libraries. This program is trivial and does not use the robot yet.

We assume you have:

First we need to prepare the main.cpp.

Open a text editor and type the following:

#include <cstdio>
#include <yarp/os/SystemClock.h>
using namespace std;
using namespace yarp::os;
int main()
printf("Starting the application\n");
int times=10;
printf("Hello iCub\n");
SystemClock::delaySystem(0.5); //wait 0.5 seconds

Save it to main.cpp.

This program will print the "Hello iCub" message for some times, waiting 0.5 seconds at every iteration of the while loop.

Let's see now how to compile this program.

Go back to your text editor and write the following file:

find_package(YARP REQUIRED)
add_executable(hello main.cpp)
# we now add the YARP libraries to our project.
target_link_libraries(hello ${YARP_LIBRARIES})

Now save this file as CMakeLists.txt, in the same directory as your main.cpp file.

This code tells cmake to produce appropriate project files to compile an executable named hello, using the file main.cpp. It also links in yarp libraries.

Now run cmake:

cmake ./

If cmake complains about not being able to localize YARP, it means that you have not followed correctly the procedure in the manual about the compilation of YARP. In particular, check that you have correctly set the environment veriables YARP_DIR. It is also possible to manually set these values, but we do not recommend it.

Now you are ready to compile this example using your favorite compiler, and execute it.

In linux: