Click here to Skip to main content
15,867,330 members
Articles / Internet of Things / Raspberry-Pi

Toolset to Cross Compile/Remote Debug Raspberry from Windows Host

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
30 Jan 2020CPOL5 min read 44.2K   298   29   21
Overview of required tools to cross compile/build/remote debug C/C++ projects on a Windows host ( 10 ) for a Raspberry PI 3B

Introduction

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

Background

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:

MSYS2

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/.

arm-linux-gnueabihf toolchain

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:

export PATH="$PATH:/c/msys32/home/RPI/SysGCC/bin"
export RPI_IP="192.168.0.144"
export RPI_USER="pi"
export RPI_PASSWORD="my_Pi_password"

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 mingw32 shell.

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.

History

  • 22nd March, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Movu robotics
Belgium Belgium
Born: 21/02/1963

Went to school, later university grade higher education but not at a university. Got a degree as "industrial engineer" in telecommunication. Then went to the army (not that I had any choice in the matter). After that,in september 1986, found a job in automation in which I wrote software for compressor controllers and multiple compressor installation management systems. I also traveled the world to install the things and explain their use to customers. I did that for 24 years and enjoyed it a lot.
Later I did some database applications using Powerbuilder and Microsoft SQL server for a few years.
Latest activity is back to real time control systems for various purposes, partially for the company I work for and sometimes as a consultant for other companies.
Later I dabbled in HVAC control systems for a while and then started doing pure consultancy work for Dekimo Experts Gent.
Was active for a few years in Avionics at ScioTeq BV in Kortrijk.

Now working at Movu robotics: making software for Pallet robots.

When I am not working I cycle, make my own wine and do some reading ( mainly science fiction ).

Comments and Discussions

 
Question'Failed to attach' Error when executing Debug (F5) Pin
Jeong sik17-May-20 16:01
Jeong sik17-May-20 16:01 
AnswerRe: 'Failed to attach' Error when executing Debug (F5) Pin
Member 160592213-Oct-20 1:12
Member 160592213-Oct-20 1:12 
QuestionHow to Manage Dependencies ? Pin
Numberfive3-Feb-20 2:54
Numberfive3-Feb-20 2:54 
Generalwhat is the configuration for Visual Studio? Pin
Southmountain2-Feb-20 6:57
Southmountain2-Feb-20 6:57 
GeneralMy vote of 5 Pin
arroway31-Jan-20 21:58
arroway31-Jan-20 21:58 
Questionhttps://www.msys2.org/ Pin
LD20-Nov-19 3:40
LD20-Nov-19 3:40 
Questionmake command result in incorrect syntax error Pin
Member 1123227126-Mar-19 13:30
Member 1123227126-Mar-19 13:30 
AnswerRe: make command result in incorrect syntax error Pin
Jeong sik11-May-20 20:50
Jeong sik11-May-20 20:50 
QuestionPi cross compile setup Pin
dseverns525-Mar-19 23:35
professionaldseverns525-Mar-19 23:35 
AnswerRe: Pi cross compile setup Pin
fd975026-Mar-19 2:05
professionalfd975026-Mar-19 2:05 
Hi,

I did not use VS but instead I used VSC (visual studio Code). By using Mingw32 and gcc I did keep a Linux like environment. The object being to create something that should be platform independent, like the earlier ESP32 experiment I did.
GeneralRe: Pi cross compile setup Pin
dseverns526-Mar-19 3:58
professionaldseverns526-Mar-19 3:58 
QuestionMSYS Link is incorrect :o( Pin
GM8JCF25-Mar-19 13:44
GM8JCF25-Mar-19 13:44 
QuestionWhere is the .ZIP mentioned? Pin
Member 1095048925-Mar-19 9:21
Member 1095048925-Mar-19 9:21 
AnswerRe: Where is the .ZIP mentioned? Pin
GM8JCF25-Mar-19 13:46
GM8JCF25-Mar-19 13:46 
AnswerRe: Where is the .ZIP mentioned? Pin
fd975026-Mar-19 1:59
professionalfd975026-Mar-19 1:59 
AnswerRe: Where is the .ZIP mentioned? Pin
fd975026-Mar-19 2:10
professionalfd975026-Mar-19 2:10 
GeneralRe: Where is the .ZIP mentioned? Pin
Member 1095048926-Mar-19 2:58
Member 1095048926-Mar-19 2:58 
GeneralRe: Where is the .ZIP mentioned? Pin
GM8JCF26-Mar-19 11:59
GM8JCF26-Mar-19 11:59 
GeneralRe: Where is the .ZIP mentioned? Pin
Jeong sik10-May-20 20:10
Jeong sik10-May-20 20:10 
GeneralRe: Where is the .ZIP mentioned? Pin
fd975010-May-20 21:31
professionalfd975010-May-20 21:31 
PraiseRe: Where is the .ZIP mentioned? Pin
Jeong sik11-May-20 20:58
Jeong sik11-May-20 20:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.