Create a new C ++ project in Eclipse CDT with the same settings as another project

Is there an easy way to create a new C ++ project that clones the settings of an existing project? When developing C ++, I like to write a lot of small tests and examples, but if my code depends on external libraries, as it often happens, I have to set fees, libraries, compiler options, etc. every time. Is there some kind of template engine?

I know about export / import of C / C ++ project settings. However, this only appears to pick up include paths and #defines for compiling C ++. The fact that it does not export a complete list of settings (compiler options, warning settings, linker options, libraries, library paths, etc.) really limits its usefulness.

In addition, you must do this separately for each launch configuration, although this is a slight inconvenience.

I usually resort to copying one test project and .project editing the .project and .cproject , and then nuking and replacing the files. But this seems like a hacking mistake.

Are there other approaches? Do I need to switch to a separate build system and generate eclipse projects from the outside in order to have what seems like pretty simple functionality?


UPDATE

I tried creating a plug-in project, but the instructions leave more than you need if you haven't already. I definitely want to figure out how to do this.

I copied and modified the sample template in several very simple ways, just for starters, but the instructions “How to register a project template with CDT” lost me from the very beginning: “1. Create an empty plug-in project from the Eclipse desktop without source folders.” I assume that this requires installing a PDE, which I did, but then I am lost. I tried the "File / New / Plug-in Project", unselected the "Create Java Project" (I assumed that it meant "Empty"). This creates a project in which there are still many things. Then I created the subdirectories as described in step 2, but I can’t figure out how to display them in Eclipse, and as a result I can’t go to the XML file of the template in step 11. Also in steps 9/10, I don’t get the template "literally called "(template)", instead it creates one with the full name of my template project.

+5
source share
2 answers

CDT has a complete Templating mechanism for creating new projects.

Basically, you expand the extension point org.eclipse.cdt.core.templates and point to the template.xml file, which has a bunch of commands that you can do. You do not need to write Java code for this, but you need to create a plug-in project.

Types of things you can do:

  • Create folders
  • Add files to the project
  • Set the parameters of the managed assembly (this is most relevant because you can set the compiler parameters and add libraries, etc.).
  • Add additional pages to the Create Project Wizard to request additional information from the user.

There is a special section in the Eclipse documentation that tells how to do this: http://help.eclipse.org/mars/topic/org.eclipse.cdt.doc.isv/guide/projectTemplateEngine/index.html

The Hello World project that comes with the CDT has its own template here: https://github.com/eclipse/cdt/blob/master/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/HelloWorldCAnsiProject/ template.xml

A small note: if you initially create your plug-in for installation as an unpacked plug-in, you can edit it, add new templates or edit the one you already made.

To go further, you can share this template plugin project with your team, and all of this will benefit from this feature.

Step by step

A step-by-step process for this (tested on Eclipse Mars.1 with installed CDT and Plug-in development tools plus an XML editor for editing template.xml)

  • Create a plug Plug-in project ( File | New | Other... | Plug-in project )

step 1

  1. Fill in the project name and click Next / Finish until completion

step 2

You should now have files on your disk that look like this in the created project:

 $ 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 
  1. Open the plug-in.xml file and follow these steps.
    • Select the Extensions tab
    • Click Add
    • Enter the extension point org.eclipse.cdt.core.templates
    • Uncheck Show only extension points [...]
    • Choose org.eclipse.cdt.core.templates from the list
    • Click Finish
    • Say Yes about adding dependencies

step 3

  1. Add the necessary settings to plugin.xml , as shown in the screenshot and the plugin.xml example in the code after.

step4

 <?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <plugin> <extension point="org.eclipse.cdt.core.templates"> <template filterPattern=".*gcc" id="com.example.cdt.templates.template1" location="template/template.xml" projectType="org.eclipse.cdt.build.core.buildArtefactType.exe"> </template> </extension> </plugin> 
  1. Now create template.xml at the location specified in plugin.xml ( template/template.xml ), with this content:
 <?xml version="1.0" encoding="ISO-8859-1"?> <template type="ProjTempl" version="1.0" supplier="Stack Overflow" revision="1.0" author="Jonah Graham" id="EXE" label="Qaru Example" description="An example for https://stackoverflow.com/questions/33092746/creating-a-new-c-project-in-eclipse-cdt-with-the-same-settings-as-another-proj." help="help.html"> <process type="org.eclipse.cdt.managedbuilder.core.NewManagedProject"> <simple name="name" value="$(projectName)" /> <simple name="artifactExtension" value="exe" /> <simple name="isCProject" value="true" /> </process> <process type="org.eclipse.cdt.core.CreateSourceFolder"> <simple name="projectName" value="$(projectName)" /> <simple name="path" value="src" /> </process> <process type="org.eclipse.cdt.core.AddFiles"> <simple name="projectName" value="$(projectName)" /> <complex-array name="files"> <element> <simple name="source" value="src/basename.c" /> <simple name="target" value="src/$(projectName).c" /> <simple name="replaceable" value="true" /> </element> </complex-array> </process> <process type="org.eclipse.cdt.ui.OpenFiles"> <simple name="projectName" value="$(projectName)" /> <complex-array name="files"> <element> <simple name="target" value="src/$(projectName).c" /> </element> </complex-array> </process> <!-- Set -Wall by checking the checkbox in the settings --> <process type="org.eclipse.cdt.managedbuilder.core.SetMBSBooleanOptionValue"> <simple name="projectName" value="$(projectName)" /> <complex-array name="resourcePaths"> <element> <simple name="id" value=".*compiler\.option\.warnings\.extrawarn.*" /> <simple name="value" value="true" /> <simple name="path" value="" /> </element> </complex-array> </process> <!-- Set -Werror by adding textual build settings --> <process type="org.eclipse.cdt.managedbuilder.core.SetMBSStringOptionValue"> <simple name="projectName" value="$(projectName)" /> <complex-array name="resourcePaths"> <element> <simple name="id" value=".*compiler\.option\.misc\.other.*" /> <simple name="value" value="-c -fmessage-length=0 -Werror" /> <simple name="path" value="" /> </element> </complex-array> </process> <!-- Add -lmylibname to libraries to link --> <process type="org.eclipse.cdt.managedbuilder.core.AppendToMBSStringListOptionValues"> <simple name="projectName" value="$(projectName)" /> <complex-array name="resourcePaths"> <element> <simple name="id" value=".*link\.option\.libs.*" /> <simple-array name="values"> <element value="mylibname" /> </simple-array> <simple name="path" value="" /> </element> </complex-array> </process> </template> 
  1. Add the source file specified in the template with any content you want in 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 
  1. Launch the Eclipse application for testing ( Run menu | Run As | Eclipse Application ). You can also right-click on a project and select Run As | Eclipse Application .

  2. In a new Eclipse launch, run the project wizard and select the new project type C:

New Project Wizard

Starting the build shows the new settings (an error is expected, because in fact I do not have a library named 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 

The hard part?

You may need to examine the .cproject file from the base project to determine the magic lines that go into the id fields. For example, in my .cproject for -Wextra I can see this:

 <option id="gnu.c.compiler.option.warnings.extrawarn.176373860" name="Extra warnings (-Wextra)" superClass="gnu.c.compiler.option.warnings.extrawarn" value="true" valueType="boolean"/> 

This translates this command into template.xml :

 <!-- Set -Wall by checking the checkbox in the settings --> <process type="org.eclipse.cdt.managedbuilder.core.SetMBSBooleanOptionValue"> <simple name="projectName" value="$(projectName)" /> <complex-array name="resourcePaths"> <element> <simple name="id" value=".*compiler\.option\.warnings\.extrawarn.*" /> <simple name="value" value="true" /> <simple name="path" value="" /> </element> </complex-array> </process> 

The identifier is sent from gnu.c.compiler.option.warnings.extrawarn.176373860 to regexp .*compiler\.option\.warnings\.extrawarn.* . The beginning is .* , So this applies to the C and C ++ compiler options, since the C ++ identifier started with gnu.cc.compiler[...] , and I get rid of the end with .* Because the number and suffix unknown to you in template.xml

Next steps

When you're done, see Starting the Eclipse Template in the Template for how to export the plug-in to running Eclipse.

+4
source

I like Jonah’s answer, it’s very informative. Recently, I used Ease and Py4J to automatically recreate a large number of projects with the cdt settings that I need. I just wanted to mention another possible way to do this type of automation.

  • Create your template project, save it in some workspace, do not have to be in the same workspace as the one you import into

  • File-> Import-> Gneral-> Existing Projects-> Select your template directory

  • Select the option "Copy projects to workspace"

  • Done

  • Rename the project if necessary, so when you import the project again as a new project, there will be no name conflicts.

+3
source

Source: https://habr.com/ru/post/1233571/


All Articles