Clion使用脚本实现对.c文件自动添加至CMakeLists.txt

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后这个都会显示未配置但并不会造成影响。 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值