如何在KDevelop中为源文件添加GPL许可证?KDevelop 5能否自动插入通用头注释?
在KDevelop中添加GPL许可证及自动插入头部注释的方法
刚好对KDevelop的这个功能比较熟悉,我来一步步告诉你怎么操作:
一、手动为单个源文件添加GPL许可证
如果你只需要给少数几个文件添加,直接手动插入就行,注意根据文件类型调整注释格式:
- 对于C/C++、Java这类用
/* */块注释的文件,在最顶部插入类似这样的内容:
/* * Copyright (C) [你的年份] [你的名字/组织名称] * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */
- 对于Python、Shell脚本这类用
#单行注释的文件,格式改成:
# Copyright (C) [你的年份] [你的名字/组织名称] # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>.
二、KDevelop 5中自动为新文件插入GPL头部注释
如果想让每个新建的源文件都自动带上GPL许可证,利用KDevelop的文件模板功能就能实现,步骤如下:
- 打开KDevelop的配置面板:点击顶部菜单栏的
Settings->Configure KDevelop... - 在左侧导航栏里找到
Source Code->File Templates - 在右侧的模板列表里,选择你需要设置的文件类型(比如C++ Source、Python Script等),然后点击
Edit按钮 - 在模板编辑界面,把GPL许可证的注释文本放到模板的最开头,还可以用KDevelop的内置变量自动填充信息,常用的变量有:
%{CURRENT_YEAR}:自动填入当前年份%{USER}:自动填入你的用户名(可以在配置里修改默认值)%{PROJECT_NAME}:自动填入当前项目的名称
举个配置好的C++源文件模板例子:
/* * Copyright (C) %{CURRENT_YEAR} %{USER} * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ #include "%{HEADER_FILE}" int main() { return 0; } - 编辑完成后点击
OK保存模板,之后每次新建对应类型的文件,KDevelop都会自动把带GPL许可证的头部插入进去。
补充:批量处理已有文件
如果你的项目里已经有一堆旧文件需要添加许可证,KDevelop没有内置的批量工具,不过可以自己写个简单的脚本(比如Python)来遍历所有源文件,检查开头是否已有许可证注释,没有的话就插入。
内容的提问来源于stack exchange,提问作者Ángel
相关产品推荐
相关产品推荐

