Skip to content

Project Setup: CLion

Jamie Smith edited this page Jun 3, 2024 · 16 revisions

This page will show you how to take an existing Mbed CE project and use it with the CLion IDE.

Installing CLion

Note: CLion is a paid product for commercial use, costing about $100 (there's a subscription, but if you cancel it you can keep using your current version forever). However, it is free for high school and college students if you fill out the application here. There is also a free 30-day trial available for everyone. I can say that it's the best C++ IDE I've personally used, and I highly recommend giving it a shot.

  1. Download the CLion installer from the download page.
  2. On Windows/Mac, run the installer and go through the install. You can then run CLion from your OS menu. On Linux, you'll get a tar file instead. Extract the tar file to Downloads, and move the folder to /opt/. Then, you will be able to run CLion by executing /opt/clion-20xx.x.x/bin/clion.sh in a terminal.
  3. The first time you open CLion you will get a license screen. You can use the "start trial" button to just use the 30 day trial, or if you have a license you should log in with your JetBrains account.
  4. You should now be at the IDE start screen!

image

Importing the Project

  1. Now, in CLion, click on Open, then browse to the root folder of your project. Then, click OK.

image

  1. You'll likely get the below trust dialog box. Click on Trust Project.

image

  1. If this is your first project, CLion will ask about setting up a toolchain. You can accept the defaults here and click Next, because the Mbed build system overrides the compiler selection.

image

  1. Now, you will get a wizard to create a build profile. For now, change the "Name" and "Build type" settings to "Develop". Also, in the "CMake Options" field, set MBED_TARGET to match your board name (e.g. -DMBED_TARGET=NUCLEO_L452RE_P for the Nucleo L452RE board). Also, optionally you can specify the upload method to use. Or, leave this unset to use the Mbed OS defaults for your board.
    • If you need Debug or Release configurations, you can come back here later and create them following the same process.

Build Profiles Screen

Finally, hit Finish.

  1. If all goes well, you should see the CMake project successfully configure and load. If not, you may need to double-check the toolchain setup guide to make sure everything is set up OK.

NOTE: When working with Mbed projects, the Tools > CMake > Reset Cache and Reload Project option is your friend if things break. If your project did not find the compilers successfully, things moved on the disk, or you're getting other errors, make it your first recourse to try this option.

image

Flashing Code

To flash code, find the entry labeled flash-<your target> in the menu at the top right. Then, click the hammer button (NOT the play button).

image

Note: Mbed OS tends to clutter this menu up with its own targets. To find your target quickly, you can just start typing with the menu open, and it will search the list. Maybe this is obvious to you but it took me like 4 years to find this...

image

Debugging

  1. First, if you haven't already, you will need to make sure the project is configured to use an upload method that supports debugging. Read about upload methods on the Upload Methods page, and select a method to use using the -DUPLOAD_METHOD= flag to CMake. Note: you can edit the CMake flags that you initially configured by going to Settings > Build, Execution, Deployment > CMake.
  2. Additionally, you will need to set the MBED_CLION_PROFILE_NAME CMake variable to match the exact name of the profile. This is needed in order to generate debug configuration files correctly. image
  3. In CLion, find the GDB debug configuration for your executable in the targets menu. This will be near the bottom and start with "GDB", then your target name. Make sure to select the one that matches your build type too -- if you are working in the Develop configuration, don't pick the Debug one!

image

  1. With the configuration selected, click the green bug button at the top. If source files have been modified, the code will be rebuilt and downloaded to the target. Then, GDB will start.

Note: If you wish to have the code stop at main(), you must set a breakpoint there before starting the debug session.

Note: With CLion, the GDB command line console is available by going to the GDB tab in the Debug panel. You can use this console in parallel with the GUI to evaluate expressions and set breakpoints.

image

Note: To reset the MCU, press the R-with-a-bar-over-it button.