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

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

问题描述

有没有直接的方式来创建一个克隆现有项目的设置的新C ++项目?在开发C ++时,我喜欢写很多小的测试和例子,但如果我的代码依赖于外部库,就像往常一样,每次都要从头开始设置include,库,编译器设置等。有没有一些模板机制?



我知道C / C ++项目设置的导出/导入。但是,这仅仅显示为C ++编译的包含路径和#define。事实上,它不会导出完整的设置(编译器选项,警告设置,链接器选项,库,库路径...)真的限制了它的有用性。



另外,您必须单独为每个运行配置进行操作,尽管这是一个轻微的不便。



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



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






更新



我已经尝试创建一个插件项目,但是如果你避开,说明会有一点点需要不要这样做。我肯定想知道如何做到这一点。



我以一些非常简单的方式复制和修改了示例模板,只是为了开始,但是如何注册一个具有CDT的项目模板指令从一开始就失去了我的注意:1.从没有源文件夹的Eclipse工作台创建一个空的插件项目。我认为这需要安装PDE,这是我做的,但是我失去了。我尝试过File / New / Plug-in Project,取消选中Create a Java Project(我认为这是Empty的意思)。这创建了一个仍然有很多东西的项目。然后,我创建了如步骤2中所述的子目录,但无法弄清楚如何使它们在Eclipse中显示,因此我无法在步骤11中浏览到模板XML文件。另外,在步骤9 / 10,我没有得到一个名为(template)的模板,而是用我的模板项目的全名创建一个模板。

解决方案

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



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



您可以执行的种类: p>


  • 创建文件夹

  • 将文件添加到项目

  • 设置管理构建设置(这是最相关的一个,因为您可以设置编译器选项和添加库等)

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



Eclipse文档有一个特别的部分,介绍如何在这里执行:


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



你现在应该有哟的文件在您创建的项目中看起来像这样的ur磁盘:

  $ 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. 取消选中仅显示扩展点[...] 复选框

    5. 选择 org.eclipse.cdt.core.templates
    6. 完成

    7. 添加依赖关系



  1. 添加th e要求的设置到 plugin.xml ,如截图所示,并在plugin.xml代码示例中给出。

  <?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版本=1.0encoding =ISO-8859-1?> 
< template type =ProjTemplversion =1.0supplier =Stack Overflow
revision =1.0author =Jonah Grahamid =EXElabel =堆栈溢出示例
description =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 =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>

<! - 设置 - 通过选中设置中的复选框 - >
< 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

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

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


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




运行构建显示新的设置(该错误是预期的,因为我实际上没有一个名为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 =额外的警告(-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 to 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 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 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天全站免登陆