CDT 拥有完整的模板机制来创建新项目。
基本上,您扩展了org.eclipse.cdt.core.templates 扩展点,它指向一个template.xml 文件,其中包含许多您可以执行的命令。您不需要为此编写任何 Java 代码,但您需要创建一个插件项目。
你可以做的事情:
- 创建文件夹
- 将文件添加到项目中
- 设置托管构建设置(这是最相关的设置,因为您可以设置编译器选项和添加库等)
- 向新建项目向导添加额外页面以提示用户提供额外信息
Eclipse 文档有一个专门的部分介绍了如何在此处进行操作:http://help.eclipse.org/mars/topic/org.eclipse.cdt.doc.isv/guide/projectTemplateEngine/index.html
CDT自带的Hello World项目模板在这里:https://github.com/eclipse/cdt/blob/master/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/HelloWorldCAnsiProject/template.xml
请注意,如果您最初创建插件以作为非打包插件安装,您可以就地编辑它,添加新模板或编辑您已经完成的模板。
更进一步,您可以与您的团队共享此模板插件项目,并从拥有此功能中受益。
一步一步
执行此操作的分步过程(在安装了 CDT 和插件开发工具以及用于编辑 template.xml 的 XML 编辑器的 Eclipse Mars.1 上进行了测试)
- 创建插件项目 (
File | New | Other... | Plug-in project)
- 填写项目名称并按
Next / Finish 直到完成
您现在应该在您创建的项目中拥有如下所示的文件:
$ 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
- 打开 plugin.xml 并执行以下操作
- 选择
Extensions 标签
- 按添加
- 键入扩展点
org.eclipse.cdt.core.templates
- 取消选中
Show only extension points [...] 复选框
- 从列表中选择
org.eclipse.cdt.core.templates
- 按
Finish
- 说
Yes添加依赖
- 将所需设置添加到
plugin.xml,如屏幕截图所示,并在之后的 plugin.xml 代码示例中给出。
<?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>
- 现在在
plugin.xml (template/template.xml) 中指定的位置创建template.xml,内容如下:
<?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>
- 在
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
启动 Eclipse 应用程序进行测试(Run 菜单 | Run As | Eclipse Application)。您也可以右键单击该项目并选择Run As | Eclipse Application.
在新运行的 Eclipse 中,启动新项目向导并选择新的 C 项目类型:
运行构建会显示新设置(该错误是预期的,因为我实际上没有名为 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
棘手的部分?
您可能需要检查基础项目中的.cproject 文件以确定id 字段中的魔术字符串。例如,在我的.cproject for -Wextra 中,我可以看到:
<option id="gnu.c.compiler.option.warnings.extrawarn.176373860" name="Extra warnings (-Wextra)" superClass="gnu.c.compiler.option.warnings.extrawarn" value="true" valueType="boolean"/>
这转化为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>
id 从gnu.c.compiler.option.warnings.extrawarn.176373860 变为正则表达式.*compiler\.option\.warnings\.extrawarn.*。开头是 .*,因此这适用于 C 和 C++ 编译器选项,因为 C++ id 将以 gnu.cc.compiler[...] 开头,而我以 .* 结尾,因为您不知道数字和后缀template.xml
后续步骤
完成后,请参阅Launching Eclipse plug in template 了解如何将插件导出到正在运行的 Eclipse 中。