You are here

Writing your first C++ program with MRPT

Tags: 

The following are the instructions for creating new applications which use the MRPT C++ library, assuming that the library has been already built in any arbitrary user directory (or, optionally, installed in the system, e.g. /usr/include, etc.). See the instructions for building the library.

You can alternatively download already compiled versions for windows here, although it is highly recommended to recompile the whole library for your specific compiler and system.

This complete example (and others) can be found in the MRPT packages, at MRPT/doc/mrpt_example*.tar.gz, or can be also downloaded here: {MRPT_DIR}/doc/mrpt_example1.tar.gz

 

1. Source files

Take a look at the list of existing libraries in MRPT and insert the appropriate includes for the libraries your code depends on (are you unsure about which MRPT libs do you need?):
 

#include <mrpt/slam.h>         // Include all classes in mrpt-slam and its dependencies
#include <mrpt/base.h>         // Include all classes in mrpt-base and its dependencies
 
using namespace mrpt;          // Global methods, and data types.
using namespace mrpt::utils;   // Select namespace for serialization, utilities, etc...
using namespace mrpt::poses;   // Select namespace for 2D & 3D geometry classes.
using namespace mrpt::slam;    // Select namespace for maps, observations, etc...
using namespace std;

If you prefer to explicitly refer to MRPT classes through their namespaces (e.g. mrpt::math::CMatrixFloat instead of CMatrixFloat), remove the using namespace statements. 

For this first example, the complete code is: {MRPT_DIR}/doc/mrpt_example1/test.cpp

 

2. The CMake build system

Using CMake is the recommended, simplest way of generating a cross-platform application that uses the MRPT libraries. Create a file named CMakeLists.txt with the following content and save it in the path of your program (containing only "test.cpp" in this example):

PROJECT(mrpt_example1)

CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
if(COMMAND cmake_policy)
      cmake_policy(SET CMP0003 NEW)  # Required by CMake 2.7+
endif(COMMAND cmake_policy)

# --------------------------------------------------------------------------
#   The list of "libs" which can be included can be found in:
#     http://www.mrpt.org/Libraries
#
#   The dependencies of a library are automatically added, so you only 
#    need to specify the top-most libraries your code depends on.
# --------------------------------------------------------------------------
FIND_PACKAGE( MRPT REQUIRED base)    # WARNING: Add all the MRPT libs used by your program: "gui", "obs", "slam",etc. 

# Declare the target (an executable)
ADD_EXECUTABLE(mrpt_example1  
	test.cpp
	)
TARGET_LINK_LIBRARIES(mrpt_example1 ${MRPT_LIBS})

# Set optimized building:
IF(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -mtune=native")
ENDIF(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")

If you are unsure about what MRPT libs to add in the "FIND_PACKAGE( MRPT REQUIRED ...)" command, read this tutorial.

When CMake is invoked through:

ccmake .

in Linux/Mac, or

cmake-gui .

in Linux/Windows, you will be asked for the path of the file MRPTConfig.cmake, which should have been created in the process of building the MRPT library. Sometimes CMake automatically finds the MRPT directory and doesn't even ask the user for the path. In Windows (for old versions of CMake) you will also have to set the variable wxWidgets_ROOT_DIR, if CMake is not able to find your wxWidgets directory automatically.

In Linux, and if MRPT has been installed in the system (/usr or /usr/local, or from synaptic), cmake will find automatically the MRPT configuration file without any additional arguments.

Note that for Linux/Mac you can see details about MRPT configuration with:

cmake .

at the build directory.
 


What's next?
You can try compiling one of the examples from the dozens available.