Skip to content
DangNhat Pham-Huu edited this page Apr 1, 2014 · 15 revisions

This wiki page is meant for those who has starting difficulties at developing C++ programs to run on RIOT OS. It is focused on the native port.

Writing your first application in C++

  1. Create a directory for your project.

  2. Create a Makefile in your project's directory. Within your project's Makefile you can define the modules you want to use. A template Makefile is available in the dist folder of the RIOT repository.

  3. Edit the file Makefile.include in the ./RIOT directory: Set g++ as compiler and modify make rules so that it is suitable for .cpp projects. E.g. replace

    `# string array of all names of c files in dir`
    `SRC = $(wildcard *.c)`
    

    with # string array of all names of cpp files in dir SRC = $(wildcard *.cpp)

    ,

    `# string array of all names replaced .c with .o` 
    `OBJ = $(SRC:%.c=${PROJBINDIR}/%.o)`
    

    with # string array of all names replaced .cpp with .o OBJ = $(SRC:%.cpp=${PROJBINDIR}/%.o)

    and

    $(BINDIR)$(PROJECT)/%.o: %.c $(PROJDEPS) $(USEPKG:%=${BINDIR}%.a) @mkdir -p ${BINDIR} @echo; echo "Compiling.... $*.c"; echo @test -d $(BINDIR)$(PROJECT) || mkdir -p $(BINDIR)$(PROJECT) $(AD)$(CC) $(CFLAGS) $(INCLUDES) -c $*.c -o $(BINDIR)$(PROJECT)/$*.o

    with

    $(BINDIR)$(PROJECT)/%.o: %.cpp $(PROJDEPS) $(USEPKG:%=${BINDIR}%.a) @mkdir -p ${BINDIR} @echo; echo "Compiling.... $*.cpp"; echo @test -d $(BINDIR)$(PROJECT) || mkdir -p $(BINDIR)$(PROJECT) $(AD)$(CC) $(CFLAGS) $(INCLUDES) -c $*.cpp -o $(BINDIR)$(PROJECT)/$*.o

    Finally you have to modify linker and compiler variable in ../boards/native/Makefile.include to use g++

  4. Create a file main.cpp (required) and other files (if needed) containing your source code in your project's directory. The file main.cpp is required because the RIOT compiler looks for a main.c or main.cpp file to start. The main.cpp file has to provide a main function according to this prototype: int main(void);

Now you can build your application for RIOT:

  • Enter the projects directory and change to your project's directory.
  • Edit the Makefile to set the variables RIOTBASE and RIOTBOARD according to where your copies of the RIOT repositories are located.
  • Dependent on your target platform set the BOARD environment variable and call make, e.g. BOARD=native make.
  • Now you can compile your code by calling make .
  • Finally see the output of the application by running ./bin/projektname

Further development & Troubleshooting

We have noticed that a certain trouble can be different toolchain and library versions for the target platforms.

Also, if you have a linker mistake, then it can be related to the missing support of some libraries. For example in our project (using RIOT version from April 2013) we could not import the following C++ headers:

<algorithm>; <array>; <atomic>; <bitset>; <condition_variable>; <complex>; <forward_list>; <fstream>; <functional>; <future>; <iomanip>; <ios>; <iostream>; <istream>; <iterator>; <locale>; <map>; <memory>; <mutex>; <ostream>; <random>; <regex>; <sstream>; <stdexcept>; <streambuf>; <string>; <system_error>; <thread>; <tuple>; <unordered_map>; <unordered_set>; <valarray>

These are only suggestions based on our experience with using C++ on RIOT in a small project. Anyone is welcome to improve, update or extend them.

Clone this wiki locally