CLion 2020.1 Help

Custom Build Targets and Applications

Custom build targets are independent of any particular build system or project model: CLion uses external tools of your choice for building the sources and cleaning build artifacts.

Custom targets are especially useful for compilation database projects. Although compile_commands.json itself lacks the information required for building and therefore running applications, custom targets and custom Run/Debug configurations help bypass this limitation.

In the following example, we will add a target to a compilation database project, get build results, and then create a custom configuration to run the executable right from CLion.

Create a custom build target

  1. Let's open a compilation database project. Click File | Open, point CLion to compile_commands.json, and choose Open as Project.

    Notice that by default the Build menu shows no available actions except for Recompile (you can use it to compile the currently opened source file):

    empty build menu for compdb projects

  2. Go to Settings / Preferences | Build, Execution, Deployment | Custom Build Targets and click icons.general.add.png to add a new target. This dialog is also accessible from the Custom Run/Debug configuration editor.

  3. Specify the following settings:

    settings for a custom build tagget

    • Toolchain

      A toolchain selected here will provide the environment and the debugger for the custom Run/Debug configuration that we will create for this target.

    • Build and Clean

      Set up the tools for building and cleaning your project: select from the list of the available tools or click the Browse button and use the artwork.studio.icons.common.edit.png and icons.general.add.png buttons to edit or add new tools.

      In our example, the Build tool (named build_with_make) calls make, and the Clean tool (clean_with_make) calls make clean:

      external tools for build and clean

  4. After saving the target, we can start using the Build Project and Rebuild Project actions. Note that in case of several custom targets, these actions are associated with the first target from the list.

    build menu for custom build targets

  5. If we select Build Project, CLion runs the build_with_make tool that calls make for the project sources:

    building a custom target

Create a custom Run/Debug configuration

Now that we have a build target, we can create a configuration to run and debug the executable.

  1. Go to Run | Edit Configurations, click icons.general.add.png and choose Custom Build Application from the list of the configuration templates.

  2. Select the newly created custom target in the Target field and specify the application binary in the Executable field. To add or edit the targets without leaving the configuration editor, click Configure Custom Build Targets.

    For our example, we also add a program argument func.cpp:

    custom run-debug configuration

  3. After saving the configuration, we can see new build actions available in the Build menu:

    custom application build menu

  4. We can now Runicons.actions.execute.png or Debugicons.actions.startDebugger.png this configuration and use all the CLion debugging and dynamic analysis features.

    Let's run our example and view the program results:

    result of running a custom run-debug configuration
Last modified: 02 July 2020