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:
| Variable | Usage |
| C_SOURCES | Specifies a list of .c sources files |
| CPP_SOURCES | Specifies a list of .cpp source files |
| CC_SOURCES | Specifies 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 |
CPP_SOURCES = my_controller.cpp my_second_file.cpp my_third_file.cpp |
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.
| Variable | Usage |
| CFLAGS | Specifies a list of flags that will be passed to the gcc/g++ compiler |
| LIBRARIES | Specifies 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 |
CFLAGS = -IC:\Users\YourName\XYZLib\include |
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 |
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 |