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
Let's open a compilation database project. Click compile_commands.json, and choose Open as Project., point CLion to
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):
Go to Custom Build Application editor.and click to add a new target. This dialog is also accessible from the
Specify the following settings:
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 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
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.
If we select Build Project, CLion runs the build_with_make tool that calls
makefor the project sources:
Create a custom run/debug configuration
Now that we have a build target, we can create a configuration to run and debug the executable.
Go to Custom Build Application from the list of the configuration templates., click and choose
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:
After saving the configuration, we can see new build actions available in the Build menu:
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: