Target/Item Templates in Advanced CMake Projects

This page describes how to customize the target and item templates used by the Advanced CMake Project Subsystem.

The templates can be used for adding new targets (e.g. executables/libraries) [1] or source/header files to an existing target [2]:

VisualGDB loads templates from 2 directories:

  • [VisualGDB directory]\CMake\templates
  • Custom directory specified in VisualGDB Project Properties -> CMake Project -> Custom target definitions directory for a particular project

Each template is stored in its own directory under either the target or item subdirectory and should contain a template.xml file with optional payload files.

The template.xml file has the following structure:

<?xml version="1.0"?>
<CMakeTargetTemplate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Name>[...]</Name>
    <FileNameBase>[...]</FileNameBase>
    <SourceFileTemplates>
        <string>[...]</string>
        <string>[...]</string>
    </SourceFileTemplates>
    <Icon>[...]</Icon>
</CMakeTargetTemplate>

Below is the list of all fields of the CMakeTargetTemplate:

Field Meaning
Name Template name as shown in the “Add new item” dialog
FileNameBase Default name for the new item (e.g. Source.cpp)
CMakeStatementTemplate For target templates, specifies the CMake statement that will be added to the CMakeLists.txt file
SourceFileTemplates List of source files that will be added to the project when the template is used. Each item inside the SourceFileTemplates tag should have a matching file in the template’s directory that will be copied into the project.
ExtraFileTemplates Similar to SourceFileTemplates, but excluded from the $$TARGETSOURCES$$ variable (see below).
Icon Template icon shown in the “Add new item” dialog. Can be one of the built-in icons (GenericFile, CPlusPlusFile, Executable, StaticLibrary, SharedLibrary, ArduinoFile, ImportedFolder, UnitTest, Qt), or a relative path to a .png file.
Dependencies List of CMake files that must be present in the project in order for the template to be shown. E.g. Qt-speciic templates can have a dependency on Qt5Config.cmake and hence won’t be shown unless this file is reported by CMake as part of the project.

The source file names or contents can use the $$TARGETNAME$$ expression to refer to the target name. E.g. a class template could use a $$TARGETNAME$$.h file with the following contents:

class $$TARGETNAME$$
{
};

The CMakeStatementTemplate can additionally refer to the list of all target’s sources via the $$TARGETSOURCES$$ expression.

You can read more about the CMake target templates in this tutorial.