Webots User Guide - chapter 5 - section 4

Webots User Guide


5.4 Using Webots Makefiles

5.4.1 What are Makefiles

The compilation of Webots C/C++ and Java controllers can be configured in the provided Makefiles. A controller's Makefile is a configuration file used by the make utility and that optionally specifies a list of source files and how they will be compiled and linked to create the executable program.

Note that Python and MATLABTM are interpreted languages and therefore they don't need Makefiles. So if you are using any of these programming languages or Visual C++ then you can ignore this section.

When using C/C++ or Java, the presence of a Makefile in the controller directory is necessary. If the Makefile is missing Webots will automatically propose to create one. This Makefile can be modified with a text editor; its purpose is to define project specific variables and to include the global Makefile.include file. The global Makefile.include file is stored in WEBOTS_HOME/resources/projects/default/controllers directory; it contains the effective build rules and may vary with the Webots version. Note that Webots Makefiles are platform and language independent.

5.4.2 Controller with Several Source Files (C/C++)

If a controller requires several C/C++ source files they need to be specified in the Makefile. The name of each source file must be listed, using one of these variables:

VariableUsage
C_SOURCESSpecifies a list of .c sources files
CPP_SOURCESSpecifies a list of .cpp source files
CC_SOURCESSpecifies a list of .cc source files

Every source file specified using these variables, will be added to the controller build. In addition dependency files will be automatically generated by the make command in order to minimize the build. Note that these variables should not be used in any language other than C or C++.

For example, if a controller has several .c source files, then this can be specified like this in the controller's Makefile:

C_SOURCES = my_controller.c my_second_file.c my_third_file.c

If a project has several .cpp source files, then this can be specified like this:

CPP_SOURCES = my_controller.cpp my_second_file.cpp my_third_file.cpp

Same thing for .cc source files. Important: the build rules require that one of the source files in the list must correspond to the controller name (i.e. controller directory name), e.g. if the controller directory is my_controller then the list must contain either my_controller.c, my_controller.cpp or my_controller.cc accordingly.

5.4.3 Using the Compiler and Linker Flags (C/C++)

These two variables can be used to pass flags to the gcc compiler or linker.

VariableUsage
CFLAGSSpecifies a list of flags that will be passed to the gcc/g++ compiler
LIBRARIESSpecifies a list of flags that will be passed to the linker

Adding an External Library (C/C++)

Webots C/C++ controllers are regular binary executable files that can easily be compiled and linked with external libraries. To add an external library it is only necessary to specify the path to the header files, and the path and name of the library in the controller's Makefile. For example the -Idir flag can be used to add a directory to search for include files. The LIBRARIES variable can be used to pass flags to the linker. For example the -Ldir flag can be used to add a directory to search for static or dynamic libraries, and the -l flag can be used to specify the name of a library that needs to be linked with the controller.

For example, let's assume that you would like to add an external library called XYZLib. And let's assume that the library's header files and .dll file are located like this (Windows):

C:\Users\YourName\XYZLib\include\XYZLib.h
C:\Users\YourName\XYZLib\lib\XYZLib.dll

Then here is how this should be specified in the Makefile:

CFLAGS = -IC:\Users\YourName\XYZLib\include
LIBRARIES = -LC:\Users\YourName\XYZLib\lib -lXYZLib

The first line tells gcc where to look for the #include<XYZLib.h> file. The second line tells gcc to link the executable controller with the XYZLib.dll and where that .dll can be found. Note that this would be similar on Linux and Mac OS X, you would just need to use UNIX-compatible paths instead. If more external libraries are required, it is always possible to use additional -I, -L and -l flags. For more information on these flags, please refer to the gcc man page.

Using Webots C API in a C++ Controller

Normally, C++ controllers use Webots C++ API. The C++ API is a set of C++ classes provided by C++ header files, e.g. #include <webots/Robot.hpp>. If you prefer, C++ controllers can use Webots C API instead. The C API is a set of C functions starting with the wb prefix and provided by C header files, e.g. #include <webots/robot.h>. To use the C API in a C++ controller you need to add this line in your controller Makefile:

USE_C_API = 1

Adding Debug Information

If you need to debug your controller, you need to recompile it with the -g flag, like this:

CFLAGS = -g

This will instruct gcc to add debugging information so that the executable can be debugged using gcc. Please find more info on debugging controllers in the next section. The default CFLAGS is empty and hence no debug information is generated.

C/C++ Code Optimization

If you need to optimize your controller code, you can use the -O1, -O2 or -O3 flags. For example:

CFLAGS = -O3

This will result in a slightly longer compilation time for a more efficient (faster) code. The default CFLAGS is empty and hence the code is not optimized by default. For more information on these flags, please refer to the gcc man page.

release 7.0.2
Copyright © 2012 Cyberbotics Ltd. All right reserved.