Using CPP on RIOT - thomaseichinger/RIOT GitHub Wiki
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.
- 
Create a directory for your project.
 - 
Create a
Makefilein your project's directory. Within your project'sMakefileyou can define the modules you want to use. A templateMakefileis available in thedistfolder of theRIOTrepository. - 
Edit the file Makefile.include in the
./RIOTdirectory: Setg++as compiler and modify make rules so that it is suitable for.cppprojects. 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 dirSRC = $(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 .oOBJ = $(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)/$*.owith
$(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)/$*.oFinally you have to modify linker and compiler variable in
../boards/native/Makefile.includeto useg++ - 
Create a file
main.cpp(required) and other files (if needed) containing your source code in your project's directory. The filemain.cppis required because the RIOT compiler looks for amain.cormain.cppfile to start. Themain.cppfile 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 
RIOTBASEandRIOTBOARDaccording to where your copies of the RIOT repositories are located. - Dependent on your target platform set the 
BOARDenvironment variable and callmake, 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 
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.