Click here to Skip to main content
14,454,989 members

Toolset to Cross Compile/Remote Debug Raspberry from Windows Host

Rate this:
5.00 (9 votes)
Please Sign up or sign in to vote.
5.00 (9 votes)
30 Jan 2020CPOL
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)

Share

About the Author

fd9750
Engineer Dekimo Experts Gent
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.

When I am not working I read, cycle and make my own wine.

Comments and Discussions

 
QuestionHow to Manage Dependencies ? Pin
Numberfive3-Feb-20 3:54
MemberNumberfive3-Feb-20 3:54 
Generalwhat is the configuration for Visual Studio? Pin
Southmountain2-Feb-20 7:57
MemberSouthmountain2-Feb-20 7:57 
GeneralMy vote of 5 Pin
arroway31-Jan-20 22:58
Memberarroway31-Jan-20 22:58 
Questionhttps://www.msys2.org/ Pin
LD20-Nov-19 4:40
MemberLD20-Nov-19 4:40 
Questionmake command result in incorrect syntax error Pin
Member 1123227126-Mar-19 14:30
MemberMember 1123227126-Mar-19 14:30 
QuestionPi cross compile setup Pin
dseverns526-Mar-19 0:35
professionaldseverns526-Mar-19 0:35 
AnswerRe: Pi cross compile setup Pin
fd975026-Mar-19 3:05
professionalfd975026-Mar-19 3:05 
GeneralRe: Pi cross compile setup Pin
dseverns526-Mar-19 4:58
professionaldseverns526-Mar-19 4:58 
QuestionMSYS Link is incorrect :o( Pin
Member 1194626425-Mar-19 14:44
MemberMember 1194626425-Mar-19 14:44 
QuestionWhere is the .ZIP mentioned? Pin
Member 1095048925-Mar-19 10:21
MemberMember 1095048925-Mar-19 10:21 
AnswerRe: Where is the .ZIP mentioned? Pin
Member 1194626425-Mar-19 14:46
MemberMember 1194626425-Mar-19 14:46 
AnswerRe: Where is the .ZIP mentioned? Pin
fd975026-Mar-19 2:59
professionalfd975026-Mar-19 2:59 
AnswerRe: Where is the .ZIP mentioned? Pin
fd975026-Mar-19 3:10
professionalfd975026-Mar-19 3:10 
GeneralRe: Where is the .ZIP mentioned? Pin
Member 1095048926-Mar-19 3:58
MemberMember 1095048926-Mar-19 3:58 
GeneralRe: Where is the .ZIP mentioned? Pin
Member 1194626426-Mar-19 12:59
MemberMember 1194626426-Mar-19 12:59 

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.

Article
Posted 22 Mar 2019

Stats

17.1K views
110 downloads
24 bookmarked