Clion 插件 "C/C++ Single File Execution" 修复cmake报错

发现clion也有leetcode插件,终于不用苦于配置vscode的C++ debug环境了,当我在Clion上安装好leetcode插件做题时发现本地调试成了问题,不会只能提交leetcode评测吧,这样还不如在网页端打开leetcode,再用CodeRunner了

我查了一下解决方案,发现有C/C++ Single File Execution这个插件,可以在 Cmakelist添加可执行文件,从而构建和运行单个 c/cpp 文件,于是我满怀期待的下载来使用,结果在使用时却报错了,如图

f145df248c6c273d8f757cee9d8f1b14/20230318171620.png

看了一下原因好像是命名不符合规范,然后我就尝试改一下名字看看是否能修复,结果发现真的是名字的问题,改成字母的数字的组合是不会报错的

e39e5c95a39608d9b5c09738f995c8cf/20230318171635.png

但是不能每次添加完都到到cmakelist文件中再手动修改一次吧,这样不如直接去修改cmakelist了。于是我找到C/C++ Single File Execution插件的开源地址,fork了一份,决定自己解决这个问题。

标点不能有?那就在插件读取文件名的时候把标点去掉。不能有中文字符?那就将中文转化为拼音,下面是主要修改的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static String cn2Spell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (char c : arr) {
if (c > 128) {
try {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(c);
}
}
return pybf.toString();
}

/** build target exeName according based on the configuration */
private String buildExeName(String exeName) {
String newExeName;

/* %FILENAME% %SAFEFILENAME% replacement */
String fileName = sourceFile.getNameWithoutExtension();
/* 将汉字转化为拼音, 清除所有符号, 只留下字母、数字、汉字3类 */
String safeFileName = cn2Spell(fileName).replaceAll("[\\pP\\p{Punct}]","");

newExeName = exeName.replace(SingleFileExecutionConfig.EXECUTABLE_NAME_FILENAME, fileName )
.replace(SingleFileExecutionConfig.EXECUTABLE_NAME_SAFEFILENAME, safeFileName);

return newExeName;
}

改完之后打包安装,完美!

这是修改后的插件自动添加的语句,cmake没有报错

5261c9df1112a74f53a6e3406e1004e0/20230318171651.png

源文件的main函数旁边也显示了小三角可以选择编译运行或debug。

3ed90b1292cda529242c0536463ed58d/20230318171714.png

修改后的插件项目地址

lorzzn/SingleFileExecutionPlugin

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2021-2025 lorzzn
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信