Is there a straightforward way to create a new C++ project that clones the settings of an existing project? When developing C++, I like to write many small tests and example
CDT has a complete Templating mechanism for creating new projects.
Basically, you extend the org.eclipse.cdt.core.templates
extension point and that points to a template.xml
file that has a bunch of commands you can do. You don't need to write any Java code for this, but you do need to create a Plug-in project.
The kinds of things you can do:
The Eclipse documentation has a special section giving a run down on how to do it here: http://help.eclipse.org/mars/topic/org.eclipse.cdt.doc.isv/guide/projectTemplateEngine/index.html
The Hello World project that comes with CDT has its template here: https://github.com/eclipse/cdt/blob/master/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/HelloWorldCAnsiProject/template.xml
A little note, if you initially create your plug-in to install as a non-packed plug-in, you can edit it in place, adding new templates or editing the one you have already done.
Going further, you can share this template plug-in project with your team and all benefit from having this feature.
The step by step process to do this (tested on Eclipse Mars.1 with CDT and Plug-in development tools installed plus an XML editor for editing the template.xml)
File
| New
| Other...
| Plug-in project
)Next
/ Finish
until doneYou should now have files on your disk that looks like this in the project you created:
$ find . -type f
./.classpath
./bin/com/example/cdt/templates/Activator.class
./.project
./src/com/example/cdt/templates/Activator.java
./.settings/org.eclipse.jdt.core.prefs
./META-INF/MANIFEST.MF
./build.properties
Extensions
taborg.eclipse.cdt.core.templates
Show only extension points [...]
checkboxorg.eclipse.cdt.core.templates
from the listFinish
Yes
to adding dependencyplugin.xml
as shown in the screenshot and given in the plugin.xml code sample after.
template.xml
in the location specified in the plugin.xml
(template/template.xml
) with these contents:
template/src/basename.c
You should now have a directory structure that looks like this:
$ find . -type f
./.classpath
./template/src/basename.c
./template/template.xml
./bin/com/example/cdt/templates/Activator.class
./.project
./src/com/example/cdt/templates/Activator.java
./.settings/org.eclipse.jdt.core.prefs
./META-INF/MANIFEST.MF
./plugin.xml
./build.properties
Launch the Eclipse Application to test (Run
menu | Run As
| Eclipse Application
). You can also right-click on the project and choose Run As
| Eclipse Application
.
In the newly running Eclipse, start a new project wizard and select your new C project type:
Running a build shows the new settings (the error is expected as I don't actually have a library called mylibname):
Building file: ../src/hello2.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -Wextra -c -fmessage-length=0 -Werror -MMD -MP -MF"src/hello2.d" -MT"src/hello2.o" -o "src/hello2.o" "../src/hello2.c"
Finished building: ../src/hello2.c
Building target: hello2
Invoking: GCC C Linker
gcc -o "hello2" ./src/hello2.o -lmylibname
/usr/bin/ld: cannot find -lmylibname
collect2: error: ld returned 1 exit status
make: *** [hello2] Error 1
You may need to examine the .cproject
file from your base project to determine the magic strings that go in the id
fields. For example, in my .cproject
for -Wextra
I can see this:
That translates to this command in the template.xml
:
The id goes from gnu.c.compiler.option.warnings.extrawarn.176373860
to regexp .*compiler\.option\.warnings\.extrawarn.*
. The beginning is .*
so that this applies to C and C++ compiler options as the C++ id would have started with gnu.cc.compiler[...]
and I get rid of the end with .*
because the number and suffix is not known to you in the template.xml
When you are done, see Launching Eclipse plug in template for how to export the plug-in into your running Eclipse.