Skip to content

VS Code Setup

Jamie Smith edited this page Aug 28, 2022 · 7 revisions

mbed-cmake and Visual Studio Code make a solid pair, and provide an editing experience equal to or better than Mbed's own IDE for zero dollars down.

Opening the Project

  1. Make sure the "C/C++", "CMake Tools", "Native Debug", and "CMake" extensions are installed.
  2. In VS Code, go to File > Open Workspace and select your project folder.
  3. Optional: Set up your CMake options. You can do this through cmake.configureSettings in settings.json. However, if your project supports multiple targets, you will need to handle this using a CMake variants file. Here's one I wrote for my project with three boards:
buildType:
  default: RelWithDebInfo
  choices:
    RelWithDebInfo:
      short: RelWithDebInfo
      long: Emit debug information but also optimize
      buildType: RelWithDebInfo
    debug:
      short: debug
      long: Emit debug information and don't optimize
      buildType: Debug
    release:
      short: Release
      long: Optimize generated code
      buildType: Release
board:
  default: GroundStationV1
  choices:
    GroundStationV1:
      short: GroundStationV1
      settings:
        TARGET: GroundStationV1
    GroundStationV2:
      short: GroundStationV2
      settings:
        TARGET: GroundStationV2
    TransponderV1:
      short: TransponderV1
      setting:
        TARGET: TransponderV1
  1. Select your kit. Click on the bottom bar where it says "No Kit Selected". Choose "[Unspecified]" from the menu.
  2. Select your variant. Use Ctrl-Shift-P and find CMake: Select Variant.

Variant list

NOTE: if you change variants later, you must do a full rebuild of your project (CMake: Delete Cache and Reconfigure) or build flags may be wrong.

  1. Configure the project. Use Ctrl-Shift-P and find CMake: Configure. Note: As of mbed-cmake 1.6.0, you should not need to select a kit, VS Code should find the compilers automatically once CMake has configured.
  2. If there's a popup asking if CMake should provide IntelliSense information, say yes!
  3. Done!

Building and Flashing

  1. Select your target. Use Ctrl-Shift-P and find CMake: Set Build Target. Then, choose the target you want from the list. Just like on the command line, targets starting with flash- flash a program to your Mbed board, while others just build the code.
  2. Start a build, either by pressing the Build button on the bottom toolbar or by pressing F7. The code should compile!

Debugging

As of mbed-cmake 1.6.0, if you have selected a configuration that supports debugging, mbed-cmake will auto-generate a launch.json that will configure VS Code for debugging. To access debugging, first build the gdbserver target. Then, while it's running, go to the Run pane on the left, select your target at the top, then click the play button to start debugging.

Clone this wiki locally