{"id":8847,"date":"2024-04-20T19:44:10","date_gmt":"2024-04-21T02:44:10","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=8847"},"modified":"2024-04-26T18:52:40","modified_gmt":"2024-04-27T01:52:40","slug":"target-item-templates-in-advanced-cmake-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/documentation\/cmake\/templates\/","title":{"rendered":"Target\/Item Templates in Advanced CMake Projects"},"content":{"rendered":"<p>This page describes how to customize the target and item templates used by the <a href=\"https:\/\/visualgdb.com\/documentation\/projects\/cmake\/\">Advanced CMake Project Subsystem<\/a>.<\/p>\n<p>The templates can be used for adding new targets (e.g. executables\/libraries) <strong>[1]<\/strong> or source\/header files to an existing target <strong>[2]:<\/strong><\/p>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/newitem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8848\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/newitem.png\" alt=\"\" width=\"1363\" height=\"340\" \/><\/a>VisualGDB loads templates from 2 directories:<\/p>\n<ul>\n<li><strong>[VisualGDB directory]\\CMake\\templates<\/strong><\/li>\n<li>Custom directory specified in <strong>VisualGDB Project Properties -&gt; CMake Project -&gt; Custom target definitions directory<\/strong> for a particular project<\/li>\n<\/ul>\n<p>Each template is stored in its own directory under either the <strong>target<\/strong> or <strong>item<\/strong> subdirectory and should contain a <strong>template.xml<\/strong> file with optional payload files.<\/p>\n<p>The <strong>template.xml<\/strong> file has the following structure:<\/p>\n<pre lang=\"xml\">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;CMakeTargetTemplate xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"&gt;\r\n    &lt;Name&gt;[...]&lt;\/Name&gt;\r\n    &lt;FileNameBase&gt;[...]&lt;\/FileNameBase&gt;\r\n    &lt;SourceFileTemplates&gt;\r\n        &lt;string&gt;[...]&lt;\/string&gt;\r\n       \u00a0&lt;string&gt;[...]&lt;\/string&gt;\r\n    &lt;\/SourceFileTemplates&gt;\r\n    &lt;Icon&gt;[...]&lt;\/Icon&gt;\r\n&lt;\/CMakeTargetTemplate&gt;<\/pre>\n<p>Below is the list of all fields of the <strong>CMakeTargetTemplate:<\/strong><\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 120px;\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">Field<\/td>\n<td style=\"width: 50%; height: 24px;\">Meaning<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">Name<\/td>\n<td style=\"width: 50%; height: 24px;\">Template name as shown in the &#8220;Add new item&#8221; dialog<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">FileNameBase<\/td>\n<td style=\"width: 50%; height: 24px;\">Default name for the new item (e.g. Source.cpp)<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">CMakeStatementTemplate<\/td>\n<td style=\"width: 50%; height: 24px;\">For target templates, specifies the CMake statement that will be added to the CMakeLists.txt file<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 50%; height: 24px;\">SourceFileTemplates<\/td>\n<td style=\"width: 50%; height: 24px;\">List of source files that will be added to the project when the template is used. Each item inside the <strong>SourceFileTemplates<\/strong> tag should have a matching file in the template&#8217;s directory that will be copied into the project.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">ExtraFileTemplates<\/td>\n<td style=\"width: 50%;\">Similar to <strong>SourceFileTemplates<\/strong>, but excluded from the $$TARGETSOURCES$$ variable (see below).<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Icon<\/td>\n<td style=\"width: 50%;\">Template icon shown in the &#8220;Add new item&#8221; dialog. Can be one of the built-in icons (<strong>GenericFile<\/strong>, <strong>CPlusPlusFile<\/strong>, <strong>Executable<\/strong>, <strong>StaticLibrary<\/strong>, <strong>SharedLibrary<\/strong>, <strong>ArduinoFile<\/strong>, <strong>ImportedFolder<\/strong>, <strong>UnitTest<\/strong>, <strong>Qt<\/strong>), or a relative path to a <strong>.png<\/strong> file.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Dependencies<\/td>\n<td style=\"width: 50%;\">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 <strong>Qt5Config.cmake<\/strong> and hence won&#8217;t be shown unless this file is reported by CMake as part of the project.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The source file names or contents can use the <strong>$$TARGETNAME$$<\/strong> expression to refer to the target name. E.g. a class template could use a <strong>$$TARGETNAME$$.h<\/strong> file with the following contents:<\/p>\n<pre lang=\"cpp\">class $$TARGETNAME$$\r\n{\r\n};<\/pre>\n<p>The <strong>CMakeStatementTemplate<\/strong> can additionally refer to the list of all target&#8217;s sources via the <strong>$$TARGETSOURCES$$<\/strong> expression.<\/p>\n<p>You can read more about the CMake target templates in <a href=\"https:\/\/visualgdb.com\/tutorials\/cmake\/targets\/\">this tutorial<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page describes how to customize the target and item templates used by the Advanced CMake Project Subsystem. The templates<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[77],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8847"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=8847"}],"version-history":[{"count":4,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8847\/revisions"}],"predecessor-version":[{"id":8852,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8847\/revisions\/8852"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=8847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=8847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=8847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}