CLion 2024.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 | Build, Execution, Deployment | Custom Build Targets and click to add a new target. This dialog is also accessible from the Custom Build Application editor.

  3. Specify the following settings:

    settings for a custom build tagget
    • Toolchain

      Toolchain selected here will provide the environment and the debugger for the custom run/debug configuration that we will create later.

      Note that compilation database projects with custom build targets use two separate toolchains:

      1. Settings | Build, Execution, Deployment | Compilation Database for resolving your project files.

      2. The target's toolchain, which defines the environment and the debugger for custom run/debug configurations.

    • 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 and 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. In the main menu, go to Run | Edit Configurations, click and choose Custom Build Application from the list of the configuration templates.

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

    Specify the application binary in the Executable field. Note that you can use remote executables here as well.

    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 Run or Debug 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: 28 June 2024