{"id":300,"date":"2015-03-02T22:27:43","date_gmt":"2015-03-02T21:27:43","guid":{"rendered":"http:\/\/www.sevenwatt.com\/main\/?p=300"},"modified":"2015-03-03T09:55:41","modified_gmt":"2015-03-03T08:55:41","slug":"eclipse-cdt-plug-ins-new-cc-project-templates","status":"publish","type":"post","link":"https:\/\/www.sevenwatt.com\/main\/eclipse-cdt-plug-ins-new-cc-project-templates\/","title":{"rendered":"Eclipse CDT Plug-ins &#8211; New C\/C++ Project Templates"},"content":{"rendered":"<p>A C\/C++ New Project Template in Eclipse CDT can be seen as a set of template\u00a0files that form the new project source code and other resources, and a description how to copy the files and setup the project. The latter is in the template.xml file. The template files can be source code (.c, .c++, .gcc, .g++), include files (.h, h++), libraries, makefiles, icons, etc. It can be actually any file in complete folder structures.<\/p>\n<h3>Macro&#8217;s<\/h3>\n<p>One of the interesting properties is that the new project\u00a0files can contain macros that get replaced upon copying. This can be used to add a copyright notice, a file name or a specifc refrence to a header named as the project.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/============================================================================\r\n\/\/ Name : $(projName).cpp\r\n\/\/ Author : $(author)\r\n\/\/ Copyright : $(copyright)\r\n\/\/ Description : Send RFM69 packets. Report received data on the serial port\r\n\/\/============================================================================ \r\n\r\n#include &quot;$(projName).h&quot;\r\nint main () {<\/pre>\n<p>When the template is selected in the New C\/C++ Project Wizard, the templateEngine expands the macro&#8217;s:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/============================================================================\r\n\/\/ Name : rf_ping.cpp\r\n\/\/ Author : sevenwatt\r\n\/\/ Copyright : (c) 2015 All rights reserved.\r\n\/\/ Description : Send RFM69 packets. Report received data on the serial port\r\n\/\/============================================================================\r\n\r\n#include &quot;rf_ping.h&quot;\r\nint main () {<\/pre>\n<blockquote><p>This is actually a cool feature.<\/p><\/blockquote>\n<p>For makefiles the same mechanism exist. But as in makefiles the same syntax <code>$(MACRO)<\/code> is already extensively used, for the templates a new syntax is introduced: <code>{{macro}}<\/code>. There is one \u00a0restriction: the source template file has to be named MakeFile, just like the target.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">LINK = {{LPCType}}.ld\r\nOBJS = {{baseName}}.o system_LPC8xx.o gcc_startup_lpc8xx.o uart.o<\/pre>\n<p>Macro&#8217;s can originate from:<\/p>\n<ul>\n<li>Eclipse C\/C++ build variables (user defined and built-in)<\/li>\n<li>Eclipse C\/C++ environment variables<\/li>\n<li>Properties on the New C\/C++ project wizard pages<\/li>\n<li>Template-defined properties in the wizard pages<\/li>\n<\/ul>\n<h3>Adding wizard pages<\/h3>\n<p>The template language has <code>propertyGroups<\/code> and <code>properties<\/code>. <code>PropertyGroups<\/code> represent a newly added wizard page, while the properties represent the UI input fields and the underlying variable the <code>property<\/code> is stored in:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;property-group id=&quot;advanced&quot; label=&quot;%LPC8xxHello.advanced.label&quot;\r\n  description=&quot;%LPC8xxHello.advanced.description&quot; type=&quot;PAGES-ONLY&quot; help=&quot;help.html&quot;&gt;\r\n     &lt;property id=&quot;toolchain&quot; label=&quot;%LPC8xxHello.toolchain.label&quot;\r\n       description=&quot;%LPC8xxHello.toolchain.description&quot; type=&quot;browsedir&quot;\r\n       mandatory=&quot;false&quot; default=&quot;%LPC8xxHello.toolchain.default&quot; persist=&quot;false&quot; \/&gt;\r\n&lt;\/property-group&gt;\r\n<\/pre>\n<p>The property has an <code>id=\"toolchain\"<\/code>. This property id can be used as macro <code>$(toolchain)<\/code> in the process steps in the template for example as a include file location.<\/p>\n<p>The following UI widgets are available as property UI, by assigning the value to attribute <code>type<\/code>:<\/p>\n<ul>\n<li><code>input<\/code>: If you want a text input box.<\/li>\n<li><code>multiline<\/code>: If you want a multi-line input box.<\/li>\n<li><code>select<\/code>: If you want a drop-down combo box.<\/li>\n<li><code>boolean<\/code>: If you want an option button.<\/li>\n<li><code>stringlist<\/code>: If you want a list box.<\/li>\n<li><code>speciallist<\/code>: If you want a list box with buttons to add and delete items to it.<\/li>\n<li><code>browse<\/code>: If you want a browse button which opens a file (not directory) selection dialog<\/li>\n<li><code>browsedir<\/code>: If you want a browse button which opens a directory selection dialog<\/li>\n<\/ul>\n<h3>Performing actions in templates: Processes<\/h3>\n<p>In templates, processes can be defined. Processes take actions such as creating a project and copying files to those projects. Examples of predefined processes are:<\/p>\n<ul>\n<li><em>org.eclipse.cdt.core.<\/em>CreateResourceIdentifier<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>ExcludeResources<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>Copy<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>AddLink<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>AppendCreate<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>Append<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>AddFiles<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>AddFile<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>CreateIncludeFolder<\/li>\n<li><em>org.eclipse.cdt.core.<\/em>CreateSourcefolder<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>NewManagedProject<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>GenerateMakeFileWithBuildDescription<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>SetMBSStringOptionValue<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>SetMBSStringListOptionValues<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>SetMBSBooleanOptionValue<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>AppendToMBSStringOptionValue<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.core.<\/em>AppendToMBSStringListOptionValues<\/li>\n<li><em>org.eclipse.cdt.ui.<\/em>OpenFiles<\/li>\n<li><em>org.eclipse.cdt.managedbuilder.gnu.ui.<\/em>SimpleGNUMakefileGenerator<\/li>\n<\/ul>\n<p>A simple example of process specification is:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;process type=&quot;org.eclipse.cdt.managedbuilder.gnu.ui.SimpleGNUMakefileGenerator&quot;&gt;\r\n    &lt;simple name=&quot;projectName&quot; value=&quot;$(projectName)&quot;\/&gt;\r\n&lt;code&gt;&lt;\/process&gt;&lt;\/code&gt;<\/pre>\n<h3>Prevent macro replacement<\/h3>\n<p>Sometimes it is unwanted to have the macro&#8217;s expanded. For example you want to expand the <code>$(projName)<\/code> in <em>main.cpp<\/em>, but do not want to expand an included make file <em>rules.mk<\/em>, because it is in the make language and includes several make macro&#8217;s. There is a solution for this in the template language. A parameter of several process, like <strong>AddFiles<\/strong> and <strong>Copy<\/strong> is <code>replaceable<\/code>. The primary semantics of <code>replaceable = \"true\"<\/code>\u00a0is:<\/p>\n<blockquote><p>Expand the macro&#8217;s<\/p><\/blockquote>\n<p>However the eclipse CDT documentation gives it the semantics:<\/p>\n<blockquote><p>Use this flag to specify whether the file should be replaced or not, if it is found in the <code>target<\/code> location<\/p><\/blockquote>\n<p>This is mostly incorrect. <strong>AddFile<\/strong> and <strong>AddFiles<\/strong> only process the macro&#8217;s. the <strong>Copy<\/strong> process also processes the macros&#8217;s conditionally, but has a quirk. When <code>replaceable = \"false\"<\/code> it fails to create target folder structure when needed. If the target folder exists, it just copies the files. The actual syntax in the process step is:\u00a0<code><code class=\"xml plain\">&lt;<\/code><code class=\"xml keyword\">simple<\/code> <code class=\"xml color1\">name<\/code><code class=\"xml plain\">=<\/code><code class=\"xml string\">\"replaceable\"<\/code> <code class=\"xml color1\">value<\/code><code class=\"xml plain\">=<\/code><code class=\"xml string\">\"true\"<\/code> <code class=\"xml plain\">\/&gt;<\/code><\/code>.<\/p>\n<h3>Available macro&#8217;s and syntaxes<\/h3>\n<p>There are some predefined macro&#8217;s in the CDT system:<\/p>\n<pre style=\"padding-left: 30px;\">projName, baseName, ........<\/pre>\n<p>Also some special eclipse syntax can be used:<\/p>\n<p><code>${workspace_loc:<i>&lt;resource path&gt;<\/i>}<\/code> for example\u00a0<code>${workspace_loc:$(projName)\/lib}<\/code><\/p>\n<p>Those built-in variables and special syntax is used in the specification of a Process step:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;process type=&quot;org.eclipse.cdt.core.AddFiles&quot;&gt;\r\n   &lt;simple name=&quot;projectName&quot; value=&quot;$(projectName)&quot; \/&gt;\r\n   &lt;complex-array name=&quot;files&quot;&gt;\r\n       &lt;element&gt;\r\n            &lt;simple name=&quot;source&quot; value=&quot;rf69.cpp&quot; \/&gt;\r\n            &lt;simple name=&quot;target&quot; value=&quot;{$(workspace_loc:$(projectName)\/lib}\/rf69.cpp&quot; \/&gt;\r\n            &lt;simple name=&quot;replaceable&quot; value=&quot;true&quot; \/&gt;\r\n       &lt;\/element&gt;\r\n   &lt;\/complex-array&gt;\r\n &lt;\/process&gt;\r\n <\/pre>\n<p>When dealing with Windows, paths may contain spaces. To indicated a path string with \u00a0spaces, double quotes can be used in the template.xml file. The problem with double quotes is that it belongs to XML syntax. The following will work in the template.xml file:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;element value=&quot;&amp;quot;$(toolchain)\/&amp;quot;&quot; \/&gt;<\/pre>\n<p>Setting project properties<\/p>\n<p>Project properties can be\u00a0set by pressing Alt-Enter in the project navigator. What can be set through the UI can also be set by the template. An example of setting additional include search paths is:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;process\r\n  type=&quot;org.eclipse.cdt.managedbuilder.core.AppendToMBSStringListOptionValues&quot;&gt;\r\n      &lt;simple name=&quot;projectName&quot; value=&quot;$(projectName)&quot; \/&gt;\r\n      &lt;complex-array name=&quot;resourcePaths&quot;&gt;\r\n          &lt;element&gt;\r\n              &lt;simple name=&quot;id&quot;\r\n                value=&quot;org.eclipse.cdt.build.core.settings.holder.incpaths&quot; \/&gt;\r\n              &lt;simple-array name=&quot;values&quot;&gt;\r\n                  &lt;element value=&quot;&amp;quot;$(toolchain)\/&amp;quot;&quot; \/&gt;\r\n              &lt;\/simple-array&gt;\r\n              &lt;simple name=&quot;path&quot; value=&quot;&quot; \/&gt;\r\n          &lt;\/element&gt;\r\n      &lt;\/complex-array&gt;\r\n&lt;\/process&gt;\r\n<\/pre>\n<p>The property that is set in this example is:\u00a0<code>org.eclipse.cdt.build.core.settings.holder.incpaths<\/code>.\u00a0 The difficult part is to figure out what the internal name of the property is that needs to be set. One way to figure out is to set a property in the property of project pages and give it a value that can be easily searched on, such as COME_FIND_ME. Then search with your favorite tool within files in the active workspace folder. I normally do this in windows with Notepad++. Most settings are by the way stored in the .cproject file, but sometimes it is stored in preferences of core components or plug-ins. This <a href=\"http:\/\/stackoverflow.com\/questions\/6711056\/eclipse-cdt-new-project-template-how-to-add-a-library\" target=\"_blank\">stackoverflow.com question (second answer)<\/a> gives some strategies to find the built-in properties.<\/p>\n<h3>List of &#8220;known&#8221; properties<\/h3>\n<table  class=\" table table-hover\" >\n<tbody>\n<tr>\n<td>Paths and symbols &#8211; includes<\/td>\n<td><em>org.eclipse.cdt.build.core.settings.holder.incpaths<\/em><\/td>\n<\/tr>\n<tr>\n<td>Paths and symbols &#8211; symbols<\/td>\n<td><em>org.eclipse.cdt.build.core.settings.holder.symbols<\/em><\/td>\n<\/tr>\n<tr>\n<td>Settings &#8211; Tool settings &#8211; Optimization<\/td>\n<td><em>gnu.c.compiler.option.optimization.flags<\/em><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><em>\u00a0gnu.c.link.option.libs<\/em><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><em>\u00a0gnu.c.link.option.nostdlibs<\/em><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><em>gnu.cpp.link.option.libs<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Selecting the project type for the template<\/h3>\n<p>The <em>plugin.xml<\/em> file defines which templates are provided by a plugin or core module. It also specifies the C\/C++ project type category. This can be<\/p>\n<table  class=\" table table-hover\" style=\"height: 122px;\" width=\"610\">\n<tbody>\n<tr>\n<td>EXE<\/td>\n<td><em>org.eclipse.cdt.build.core.buildArtefactType.exe<\/em><\/td>\n<\/tr>\n<tr>\n<td>Shared Lib<\/td>\n<td><em>org.eclipse.cdt.build.core.buildArtefactType.sharedLib<\/em><\/td>\n<\/tr>\n<tr>\n<td>Static Lib<\/td>\n<td><em>org.eclipse.cdt.build.core.buildArtefactType.staticLib<\/em><\/td>\n<\/tr>\n<tr>\n<td>Makefile<\/td>\n<td><em>org.eclipse.cdt.build.makefile.projectType<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>By setting the projectType attribute in the template element, the proper category the template shows is selected:\u00a0<code>projectType=\"org.eclipse.cdt.build.makefile.projectType\"<\/code>. A full specification of a plugin delivering one template would look like this in\u00a0<em>plugin.xml:<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;plugin&gt;\r\n  &lt;extension point=&quot;org.eclipse.cdt.core.templates&quot;&gt;\r\n      &lt;template\r\n        filterPattern=&quot;.*g\\+\\+&quot;\r\n        id=&quot;com.sevenwatt.templates.LPC8xxHelloWorld&quot;\r\n        location=&quot;templates\/projecttemplates\/LPC8xxHelloWorld\/template.xml&quot;\r\n        projectType=&quot;org.eclipse.cdt.build.makefile.projectType&quot;&gt;\r\n      &lt;\/template&gt;\r\n  &lt;\/extension&gt;\r\n&lt;\/plugin\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A C\/C++ New Project Template in Eclipse CDT can be seen as a set of template\u00a0files that form the new project source code and other resources, and a description how to copy the files and setup the project. The latter is in the template.xml file. The template files can be source code (.c, .c++, .gcc, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-eclipse-cdt"],"_links":{"self":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/comments?post=300"}],"version-history":[{"count":30,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/posts\/300\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sevenwatt.com\/main\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}