SoftBank Robotics documentation What's new in NAOqi 2.5?

C++ SDK - Hello world (step by step)

Prerequisites

You should have some good knowledges in C++. However, some C++11 new features independent of NAOqi SDK are detailed in this tutorial.

Set your environment

To set your environment in order to be ready to code:

Step Action

Install qiBuild.

Why?

qiBuild is the tool used for building software.

How?

Follow the step by step guide: Installing qiBuild.

Create the directory where you want to create your module.

How?

Go in your worktree directory, inside it create another directory where you want to write your new module.

For this tutorial, this directory is named: octopus.

Result

We have this tree: my_worktree/octopus.

Create the qiproject.xml file.

Why?

The file qiproject.xml is used by qiBuild to know how to manage dependencies between the libraries your project uses.

The minimum content of this file should be:

  • The maintainer of the project
  • The name of your project.

How?

In your octopus directory, create the file qiproject.xml with a content organized as follows:

<project name="octopus">
    <maintainer email="jdoe@mycompany.com">John DOE</maintainer>
</project>

Note: The name of your project directory and the project name in the file qiproject.xml do not necessarily have to be the same.

Create the CMakeLists.txt file.

Why?

The file CMakeLists.txt is used to know how to build the source code corresponding to your own project.

How?

In your octopus directory, create the file CMakeLists.txt with the following content:

cmake_minimum_required(VERSION 2.8)
project(octopus)

Note: the name of your project in the CMakeLists.txt file has no impact.

Your first configuration.

Why?

The configuration prepares your project to be builded.

How?

To create the new build directory where your libraries and executables will be builded later, write, in your octopus directory:

qibuild configure --release

Where --release specifies in release mode. If you do not precise --release, the configuration will be done by default in debug mode.

Result

Look in your project directory, qiBuild just created for you a brand new:

build-<custom> directory

Where <custom> corresponds to your system configuration, for example sys-linux_x86_64 if you are on Linux 64 bits.

Your first code

In this part, you will learn how to write and build your first “Hello World” with qibuild.

Step Action

Write your first code

Well, we have a directory where our code will be builded in the future, but we have no code at all for the moment. Let’s write it! Because we are very original, let’s write a nice Hello World!

Let’s create the following file called main.cpp in a new sub-directory:

src:

In the src directory, create the file main.cpp with the following content:

#include <iostream>

int main()
{
  std::cout << "Hello, world!" << std::endl;
  return 0;
}

Build your first code (first try)

Simply write:

qibuild make

Result

Perfect, no error.

But, wait, I cannot see my hello world program anywhere! Actually, it’s normal, we never said to qiBuild what file it should build.

To do so, just add in the file CMakeLists.txt the line:

qi_create_bin(octopus-bin "src/main.cpp")

This lines said to qiBuild it should build an executable file named octopus-bin from the source file src/main.cpp.

Your CMakeLists.txt file should now looks like this:

cmake_minimum_required(VERSION 2.8)
project(octopus)

qi_create_bin(octopus-bin "src/main.cpp")

Build your first code (second try)

Let’s compile again:

qibuild make

Oh no! qiBuild shows this beautiful error message:

../../_images/cpp_dummies_qibuilderror01.png

Sounds like qiBuild cannot use the qi_create_bin command.

Actually that’s normal, because qiBuild does not know where this command is available.

To correct that, just add to your CMakeLists.txt file the line:

find_package(qibuild)

This line tells to qiBuild that is should find and use the package qiBuild which contains the command qi_create_bin.

Your file CMakeLists.txt should now look like this:

cmake_minimum_required(VERSION 2.8)
project(octopus)
find_package(qibuild)

qi_create_bin(octopus-bin "src/main.cpp")

Build your first code (third and last try)

Let’s compile again:

qibuild make

This time it is OK, you will find your brand new executable file in the build-<custom>/sdk/bin directory, where: <custom> corresponds to your system configuration, for example sys-linux_x86_64.

Congratulations!

You could execute it just by writing:

./octopus-bin.

Congratulations! You wrote your first Hello World with qiBuild!