ARM GCC Cross Compilation in Visual Studio

点这里看中文版

In Visual Studio 2017 15.5 Preview 2 we are introducing support for cross compilation targeting ARM microcontrollers. To enable this in the installation choose the Linux development with C++ workload and select the option for Embedded and IoT Development. This adds the ARM GCC cross compilation tools and Make to your installation.

Our cross compilation support uses our Open Folder capabilities so there is no project system involved. We are using the same JSON configuration files from other Open Folder scenarios and have added additional options to support the toolchains introduced here. We hope that this provides flexibility for many styles of embedded development. The best way to get started with this and understand the capabilities is with a project exported from the ARM mbed online compiler. We’ll cover the basics here, to learn more about the online compiler see ARM’s tutorials, and you can sign up for an account here.

ARM’s online compiler lets you select your target platform and configures your project accordingly. I’m using an ST Nucleo-F411RE, but any board supported by the compiler should be fine. Click New, and in the dialog that opens choose a template to get started with. I chose “Blinky LED test for ST Nucleo boards”.

ARM new project dialog

Then in the Program Workspace select the program, right click and choose Export Program.

In the Export program dialog choose GCC (ARM Embedded) for the Export Target.

This will download a zip file to your computer. Extract the contents of that file to where you like and open Visual Studio. Now open it with File > Open > Folder. We need to inform VS of what type of C++ code this is, so go the Project menu and select Edit Settings > CppProperties.json. Modify the contents so there is only one entry that matches the below.

{
  "configurations": [
    {
      "inheritEnvironments": [
        "gcc_arm"
      ],
      "name": "gccarm",
      "includePath": [
        "${env.INCLUDE}"
      ],
      "defines": [

      ]
    }
  ]
}

The environment definition for gcc_arm sets the path to the ARM GCC toolchain that VS installed. It also triggers scanning of the subfolder named mbed to properly set include paths for use in IntelliSense. You will see this when you open main.cpp. Everything you expect to work will, auto completion, go to definition, peek definition, etc.  You can specify additional include directories, but note that subdirectories when present must also be explicitly declared at this time.

Open folder also supports things like syntax highlighting for makefiles, so go ahead and open it to see that. To compile the project right click the Makefile and select build.

You’ll see the build output in the output window.

You will now see a new BUILD output folder in the Solution Explorer. Within that is your output binary which is your program name .bin you can flash onto your board.

One of the great things about mbed boards is how easy they are to flash with that binary. When connected to your PC they show up as a USB mass storage device, so you simply drag and drop the bin file to the drive, the board resets and your program runs. If you want to control this from Visual Studio you can use tasks. Select the binary, right click and choose Configure Tasks. This will create a file called tasks.vs.json where you can configure tasks that will show up in the context menu for the file it is configured for. The example task shown below will copy the binary specified to the D drive, you can modify this to match your system. The command ${env.COMSPEC} provides a command prompt environment that the arguments are passed to.

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskName": "flash",
      "appliesTo": "BUILD/Nucleo_blink_led.bin",
      "type": "launch",
      "command": "${env.COMSPEC}",
      "args": [
        "copy BUILD\\Nucleo_blink_led.bin D:"
      ]
    }
  ]
}

What’s next

Download the Visual Studio 2017 Preview, install the Linux C++ Workload, select the option for Embedded and IoT Development and give it a try with your projects.

We have prototyped debugging support and expect to add that in a future release. It uses the same launch.vs.json format as other Open Folder scenarios, so it is possible to use in this release if you know what needs to be specified for your board and gdbserver.

We’re actively working on additional support for embedded scenarios. Your feedback here is very important to us. We look forward to hearing from you and seeing the things you make.

The best way to reach us is via our GitHub hosted issue list, directly via mail at vcpplinux-support@microsoft.com or find me on Twitter @robotdad.