iCub-main
The classic hello world

Goal

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:

  • installed and compiled YARP
  • correctly set YARP_DIR

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;
while(times--)
{
printf("Hello iCub\n");
SystemClock::delaySystem(0.5); //wait 0.5 seconds
}
printf("Goodbye!\n");
}
int main(int argc, char *argv[])
Definition: main.cpp:31

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:

project(HELLO_ICUB)
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:

make
./hello