Using a Cross-compiler for Raspberry PI

This tutorial shows how to develop for Raspberry PI using a cross compiler in Visual Studio.

Before we begin, ensure that VisualGDB is installed.

  1. Start Visual Studio. Go to File->New->Project and select VisualGDB->Linux Project Wizard. Choose a name and location for the project and press ‘OK’ when done.01-prj
  2. Choose ‘Create a new project‘ -> ‘Application‘. If you are familiar with the classic Visual Studio C++ projects, select ‘MSBuild‘. Otherwise, choose ‘CMake‘ and press ‘Next’:02-prjtype
  3. Choose ‘Build the project locally with a cross-compiler, then open the cross-toolchain selector and click “Download more toolchains”:03-getmore
  4. Choose the toolchain to be downloaded and installed.raspitc
  5. Specify the installation directory. For the best compatibility with GDB and Make, it is recommended to avoid paths with spaces when installing toolchains:
  6. VisualGDB will automatically download and install the toolchain:05-download
  7. Once the installation of the toolchain completes, the installed toolchain will appear in the toolchain selector. If you are using VisualGDB 5.4 or later with the latest Raspberry Pi toolchain, you can click on the SD card icon to the right from the toolchain selector to download the compatible SD card image from the Raspberry Pi website automatically:sd
  8. Ensure your Raspberry Pi is running from the correct SD card image and that it’s connected to your network. Now we will configure VisualGDB to target it by creating a new SSH connection for it:newconn
  9. Choose the hostname or ip of the board together with the user name and password. Here we use the default user name pi and password raspberry. It is recommended to check the “setup public key” checkbox, so that VisualGDB will automatically generate an public/private keypair, store it in your Windows account’s key container and setup the Raspberry PI box to use it. Press on ‘Create’ to make the connection.07-sshdetailsIf you don’t enable public key authentication, VisualGDB will remember your password for this connection. The stored passwords are encrypted using a key stored in your Windows account. Thus, the password will only be available once you login using your Windows account.
  10. Once the connection has been made, press on ‘Finish’ in the wizard:finish
  11. Build the project. VisualGDB will use the local toolchain to build it.09-project
  12. Set a breakpoint in the main function. Start debugging. VisualGDB will deploy your executable to the Raspberry PI board and the breakpoint will hit:10-debugYou can use the regular Visual Studio project properties to change various parameters (e.g. include directories or preprocessor macros). For advanced settings, such as unit tests, dynamic program analysis, profiling, etc. use the VisualGDB Project Properties window.
  13. If you want to add support for additional headers/libraries, install them on Raspberry Pi via apt-get and then open VisualGDB Project Properties on the Build Settings page and click “Synchronize sysroot”:11-resync
  14. Proceed with the suggested directory list and VisualGDB will automatically ensure that all the libraries installed on Raspberry Pi become available when using the cross-toolchain:12-confirm

If you would like to use CMake instead of MSBuild, try out the Advanced CMake Project Subsystem that lets you conveniently use the Visual Studio GUI for managing CMake projects.