The one line description explains most of it: I have a Raspberry PI 3B for which I have wanted to build some C/C++ projects. Oddly enough, how to do that from a Windows (10) host to do the cross compilation/ build and also do remote debugging from the host computer is very hard to find. There is lots of info out there but no summary of how to set up things to make an example "
Hello world" from a Windows host on a remote Raspberry target. I think the main reason for that is that "C/C++" is not very popular on the Raspberry PI, Python appears to be much more popular.
As I have been able to assemble all the necessary stuff to pull it off, I decided to make the summary myself. My Raspberry PI is running the Raspbian Linux distro so it is probably not a bad example on how to set things up on other development boards running various Linux distros as well
The last couple of years, I have done quite a lot of software cross development using a combination of Eclipse and various gcc/g++ development toolchains on multiple platforms (Bare metal/FreeRTOS/Linux on all sorts of processors: ARMcortex M0+...dual core ARM9, ESP32, Power5,...) so I do have a basic knowledge on how these things work.
Then, I recently went from an Eclipse/ESP32 toolchain combination to a Visual Studio Code/ESP32 toolchain which turned out to be much easier to set up than the Eclipse/ESP32 toolchain one. At the time, I had some ESP32 hardware available so I could test that setup on a Windows(10), Linux and an OSX host machine and it worked on all platforms.
As I now have only a Window 10 host and a RaspBerry PI3B running Raspbian as target, I have tried and managed to get it working from my Windows 10 host but all in all, it should be pretty much platform independent as a lot of the scripting relies on Environment variables which need a slightly different setup on the various platforms but they are common to all the possible platforms, particularly because I use the Mingw32 shell on my Windows machine.
Using the Code
The ZIP file contains the entire project and consists of very few files. Just put it anywhere you like. Note; once you unzip it, you will find that there is a subdirectory under subdirectory main with the name test and a file test.c in it, the latter two can be ignored as they are only there to test if files in subdirectories would build as well; they do.
To build the project, various tools need to be installed first so here is what needs to installed:
This provides Linux like terminal shell operation. Typically, it installs in directory C:\msys32.
Run Mingw32.exe in that directory to start a shell. The install can be found at https://www.msys2.org/.
This is the required toolchain to build C/C++ projects for the raspberry PI. If you want to build for the latest, at the time of writing, "full stretch" edition of Raspbian, you need version 6.0.3 of it. Prebuilt versions of it can be found for Linux, OSX and Windows. The one I used can be found here: http://gnutoolchains.com/raspberry/. I installed it here: C:\msys32\home\RPI\SysGCC.
Once installed, add a script file called RPI-gcc_toolchain.sh in directory C:\msys32\etc\profile.d. The content should look something like this:
Of course, the IP address depends on what your local router provides, setting it up to always provide the same IP address for your RPI's MAC address is definitely a good idea. Once done, the extended
PATH data and the extra environment variables will be defined each time you run a
Extra Environment Variable
To enable some features used by VSCODE and the makefile in the example project, add environment variable
RPIDEV_LOC to your Windows setup and set it to c:/msys32/home/RPI/SysGCC (Using Control Panel-> System->Advanced settings->Environment variables).
Command Line Build Test
Once the above is done, you should be able to test building the project:
- Start a
mingw32 shell and navigate to the root directory of the example project.
- Type make to start the cross compile/build and you should see it building files hello_world_main.c and test.c.
- If all is well, the final action is that it will produce the Hello_world_c file which is an ELF file that can run on the Raspberry PI. If that works, we can get going with Visual Studio code.
Installing Visual Studio Code and Extensions
For those who have not got Visual Studio Code yet, you need to install various items:
- Visual Studio code itself, to be found at https://code.visualstudio.com/
- Extension C/C++ by Microsoft to get C/C++ support intellisense, etc.
- Extension Native-Debug to get Cross platform GDB debug support
Using Visual Studio Code to Build and Debug the Example Code
Once Visual Studio Code and the extensions are installed, you can start by opening the project's folder. Within that folder, there is .vscode folder, in it are all the necessary JSON configuration files which provide all of the necessary build tools, etc. the build tool list pops up by pressing Ctrl+Shift+B.
The number of build commands is not long, apart from the usual clean, build and fast build tools the most important one is the Transfer... tool which transfers the file to execute to the RPI and starts the
GDBSERVER on that unit with it
Actual debugging is started by pressing F5 which will attempt to connect to the RPI's Recently started
GDBSERVER. From that point on, you can do complete debugging; setting breakpoints, investigating variables, etc. just as if the application was running on your host system itself.
So, that's it. I have done quite a bit of CLI development work in the pre GUI days. Some embedded Linux applications work, some windows development work and tons of other embedded work, with or without some form of OS. I really liked doing this as it ties a lot of stuff neatly together. I just hope this can be of use to someone who needs a solution without having to do all the digging personally.
- 22nd March, 2019: Initial version