1.下载插件
2.编写脚本文件
# -*- coding: utf-8 -*-
import os
# 获取所有的 C 源文件
source_files = []
for root, _, files in os.walk('.'):
for file in files:
if file.endswith('.c'):
source_files.append(os.path.join(root, file).replace('\\', '/'))
# 读取现有的 CMakeLists.txt
with open('CMakeLists.txt', 'r', encoding='utf-8') as file:
cmake_content = file.readlines()
# 找到源文件部分并更新
try:
start_index = cmake_content.index('# Source files start\n')
end_index = cmake_content.index('# Source files end\n')
# 构建新的源文件列表
new_source_list = [f'add_executable({os.path.splitext(os.path.basename(f))[0]} {f})\n' for f in source_files]
# 更新 CMakeLists.txt
cmake_content = cmake_content[:start_index+1] + new_source_list + cmake_content[end_index:]
with open('CMakeLists.txt', 'w', encoding='utf-8') as file:
file.writelines(cmake_content)
except ValueError as e:
print("标记 '# Source files start' 或 '# Source files end' 不在 CMakeLists.txt 文件中")
print(str(e))
3. 找到插件并进行配置
4.如果以上没有实现自动添加,提示python解释器问题再做如下
尽管每次重启clion后这个都会显示未配置但并不会造成影响。