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
Makefile
in your project's directory. Within your project'sMakefile
you can define the modules you want to use. A templateMakefile
is available in thedist
folder of theRIOT
repository. -
Edit the file Makefile.include in the
./RIOT
directory: Setg++
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 useg++
-
Create a file
main.cpp
(required) and other files (if needed) containing your source code in your project's directory. The filemain.cpp
is required because the RIOT compiler looks for amain.c
ormain.cpp
file to start. Themain.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
andRIOTBOARD
according to where your copies of the RIOT repositories are located. - Dependent on your target platform set the
BOARD
environment 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.