在Eclipse CDT中创建与另一个项目相同设置的新C ++项目 [英] Creating a new C++ Project in Eclipse CDT with the same settings as another project

查看:1282
本文介绍了在Eclipse CDT中创建与另一个项目相同设置的新C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来创建一个新的C ++项目,克隆现有项目的设置?当开发C ++时,我喜欢写许多小测试和例子,但如果我的代码依赖于外部库,因为他们经常做,我必须设置包括,库,编译器设置等,从头开始每次。是否有某种模板机制?



我知道C / C ++项目设置的导出/导入。但是,这似乎只是提取包括C ++编译的路径和#define。它不导出完整的设置(编译器选项,警告设置,链接器选项,库,库路径,...)的事实真的限制了它的用途。



此外,您必须为每个运行配置单独进行,但这是一个小小的不便。



我通常使用的是复制一个测试项目和手动编辑 .project .cproject 文件,然后nuking并替换文件。但这似乎是一个容易出错的黑客。



有其他方法吗?我需要切换到单独的构建系统并在外部生成Eclipse项目,以便具有类似非常基本的功能吗?






UPDATE



我已经尝试创建一个插件项目,但是如果你没有指望,以前做过这个。我真的想弄清楚如何做到这一点。



我以一些非常简单的方式复制和修改了示例模板,只是为了开始,但如何注册带有CDT的项目模板指令从一开始就让我失望:1.从Eclipse工作台创建一个没有源文件夹的空插件项目。我假设这需要安装PDE,我做了,但是我失去了。我试过文件/新/插件项目,取消选择创建Java项目(我假设这是什么意思是空)。这创建了一个仍然有很多东西的项目。然后,我按照步骤2中所述创建了子目录,但无法弄清如何让它们显示在Eclipse中,因此我无法浏览到步骤11中的模板XML文件。此外,在步骤9 / 10,我没有得到一个模板字面命名(模板)' - 而是它创建一个与我的模板项目的全名。

解决方案

CDT有一个完整的模板机制来创建新项目。



基本上,你扩展 org.eclipse.cdt.core.templates 扩展点,并指向一个 template.xml 文件,它有一堆命令,你可以做。您不需要为此编写任何Java代码,但您需要创建一个插件项目。



您可以做的事情:




  • 创建文件夹

  • 将文件添加到项目

  • 设置

  • 向新建项目向导添加额外页面以提示用户提供额外信息
  • >


Eclipse文档中有一个特殊部分,介绍如何在此处执行此操作:


  1. 填写项目名称,然后按下一步 / 完成



您现在应该在您创建的项目中的磁盘上有如下所示的文件:

  $ 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. 打开plug-in.xml并执行以下操作


    1. 选择扩展标签

    2. 按添加

    3. 键入扩展点 org.eclipse.cdt.core.templates

    4. code>仅显示扩展点[...] 复选框

    5. 选择 org.eclipse.cdt.core.templates

    6. c $ c>是添加依赖项



  1. 将所需设置添加到插件。

 <?xml version =1.0encoding =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. 现在创建 plugin.xml template / template.xml >)与以下内容:



 <?xml version =1.0encoding =ISO-8859-1?> 
< template type =ProjTemplversion =1.0supplier =Stack Overflow
revision =1.0author =Jonah Grahamid =EXElabel =
description =http://stackoverflow.com/questions/33092746/creating-a-new-c-project-in-eclipse-cdt-with-the-same-settings-as-another-项目。
help =help.html>


< process type =org.eclipse.cdt.managedbuilder.core.NewManagedProject>
< simple name =namevalue =$(projectName)/>
< simple name =artifactExtensionvalue =exe/>
< simple name =isCProjectvalue =true/>
< / process>

< process type =org.eclipse.cdt.core.CreateSourceFolder>
< simple name =projectNamevalue =$(projectName)/>
< simple name =pathvalue =src/>
< / process>

< process type =org.eclipse.cdt.core.AddFiles>
< simple name =projectNamevalue =$(projectName)/>
< complex-array name =files>
< element>
< simple name =sourcevalue =src / basename.c/>
< simple name =targetvalue =src / $(projectName).c/>
< simple name =replaceablevalue =true/>
< / element>
< / complex-array>
< / process>

< process type =org.eclipse.cdt.ui.OpenFiles>
< simple name=projectNamevalue =$(projectName)/>
< complex-array name =files>
< element>
< simple name =targetvalue =src / $(projectName).c/>
< / element>
< / complex-array>
< / process>

<! - 通过选中设置中的复选框设置-Wall - >
< process
type =org.eclipse.cdt.managedbuilder.core.SetMBSBooleanOptionValue>
< simple name =projectNamevalue =$(projectName)/>
< complex-array name =resourcePaths>
< element>
< simple name =idvalue =。* compiler\.option\.warnings \.extrawarn。*/>
< simple name =valuevalue =true/>
< simple name =pathvalue =/>
< / element>
< / complex-array>
< / process>

<! - 通过添加文本构建设置设置-Werror - >
< process
type =org.eclipse.cdt.managedbuilder.core.SetMBSStringOptionValue>
< simple name =projectNamevalue =$(projectName)/>
< complex-array name =resourcePaths>
< element>
< simple name =idvalue =。* compiler\.option\.misc \.other。*/>
< simple name =valuevalue = - c -fmessage-length = 0 -Werror/>
< simple name =pathvalue =/>
< / element>
< / complex-array>
< / process>

<! - 将-lmylibname添加到要链接的库 - >
< process
type =org.eclipse.cdt.managedbuilder.core.AppendToMBSStringListOptionValues>
< simple name =projectNamevalue =$(projectName)/>
< complex-array name =resourcePaths>
< element>
< simple name =idvalue =。* link\.option\.libs。*/>
< simple-array name =values>
< element value =mylibname/>
< / simple-array>
< simple name =pathvalue =/>
< / element>
< / complex-array>
< / process>
< / template>




  1. 添加模板中列出的源文件 template / src / basename.c

  2. 中的任何内容

现在有一个如下所示的目录结构:

  $ 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. 启动Eclipse应用程序以测试(运行 menu | 运行为 | Eclipse应用程序)。您也可以右键单击项目,然后选择运行方式 | Eclipse应用程序


  2. 在新运行的Eclipse中,启动一个新的项目向导,项目类型:




运行构建显示新设置(预期错误,因为我实际上没有名为mylibname的库):

 构建文件:../src/hello2.c 
调用:GCC C编译器
gcc -O0 -g3 -Wall -Wextra -c -fmessage-length = 0 -Werror -MMD - MP -MFsrc / hello2.d-MTsrc / hello2.o-osrc / hello2.o../src/hello2.c
成品:../src/ hello2.c

构建目标:hello2
调用:GCC C链接器
gcc -ohello2./src/hello2.o -lmylibname
/ usr / bin / ld:找不到-lmylibname
collect2:错误:ld返回1退出状态
make:*** [hello2]错误1



棘手的部分?



您可能需要检查 .cproject 文件,以确定 id 字段中的魔术字符串。例如,在 .cproject 中为 -Wextra 我可以看到:

 < option id =gnu.c.compiler.option.warnings.extrawarn.176373860name =Extra warnings(-Wextra )superClass =gnu.c.compiler.option.warnings.extrawarnvalue =truevalueType =boolean/> 

这转换为 template.xml

 <! - 通过选中设置中的复选框设置 -  - > 
< process
type =org.eclipse.cdt.managedbuilder.core.SetMBSBooleanOptionValue>
< simple name =projectNamevalue =$(projectName)/>
< complex-array name =resourcePaths>
< element>
< simple name =idvalue =。* compiler\.option\.warnings \.extrawarn。*/>
< simple name =valuevalue =true/>
< simple name =pathvalue =/>
< / element>
< / complex-array>
< / process>

ID来自 gnu.c.compiler.option.warnings.extrawarn .176373860 到regexp 。* compiler\.option\.warnings\.extrawarn。* 。开始是。* ,因此这适用于C和C ++编译器选项,因为C ++ ID将以 gnu.cc.compiler [.. 。] ,我用摆脱了结尾,因为 template.xml



后续步骤



完成后,请参阅启动Eclipse插件模板,了解如何将插件导出到运行中Eclipse。


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 examples, but if my code depends on external libraries, as they often do, I have to set the includes, libraries, compiler settings, etc., up from scratch each time. Is there some sort of template mechanism?

I know about Export/Import of C/C++ Project Settings. However, this only appears to pick up include paths and #defines for the C++ compilation. The fact that it doesn't export the full slate of settings (compiler options, warning settings, linker options, libraries, library paths, ...) really limit its usefulness.

Also, you have to do it separately for each run configuration, though this is a minor inconvenience.

What I usually resort to is copying one test project and manually editing the .project and .cproject files and then nuking and replacing the files. But this seems like an error-prone hack.

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


UPDATE

I've tried creating a Plug-in Project but the instructions leave more than a little to be desired if you haven't done this before. I definitely want to figure out how to do this.

I copied and modified the sample template in some very simple ways, just to get started, but the "How to register a project template with CDT" instructions lost me from the start: "1. Create an empty plug-in project from the Eclipse workbench without the source folders." I assume this requires installing the PDE, which I did, but then I'm lost. I tried "File / New / Plug-in Project", deselected "Create a Java Project" (I assumed this was what was meant by "Empty"). This creates a project that still has a lot of stuff in it. I then created the subdirectories as described in step 2, but can't figure out how to get these to show up in Eclipse, and as a result I can't browse to the template XML file in step 11. Also, in steps 9/10, I don't get a template 'literally named "(template)"' - instead it creates one with the full name of my template project.

解决方案

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:

  • Create folders
  • Add files to a project
  • Set Managed Build settings (this is the one most relevant because you can set compiler options and add libraries, etc)
  • Add extra pages to the New Project Wizard to prompt user for extra information

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.

Step-by-step

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)

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

  1. Fill in a project name and press Next / Finish until done

You 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

  1. Open the plug-in.xml and do the following

    1. Select the Extensions tab
    2. Press Add
    3. Type the extension point org.eclipse.cdt.core.templates
    4. Un-check the Show only extension points [...] checkbox
    5. Select the org.eclipse.cdt.core.templates from the list
    6. Press Finish
    7. Say Yes to adding dependency

  1. Add the required settings to the plugin.xml as shown in the screenshot and given in the plugin.xml code sample after.

<?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 the template.xml in the location specified in the plugin.xml (template/template.xml) with these contents:

<?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="Stack Overflow Example"
    description="An example for http://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 listed 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 to test (Run menu | Run As | Eclipse Application). You can also right-click on the project and choose Run As | Eclipse Application.

  2. 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

The tricky part?

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:

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

That translates to this command in the 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 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

Next steps

When you are done, see Launching Eclipse plug in template for how to export the plug-in into your running Eclipse.

这篇关于在Eclipse CDT中创建与另一个项目相同设置的新C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆