!文章内容如有错误或排版问题,请提交反馈,非常感谢!
为什么要使用Clion?
使用Clion的优点
- 开发效率提升
- 智能代码支持,CLion作为专业的C/C++ IDE,提供智能代码补全、重构工具、实时错误检测等功能,显著减少语法错误和调试时间。对于51单片机常用的寄存器操作(如TMOD、TH0等),CLion能自动识别头文件定义,提高编码效率
- 集成化编译与烧录,PlatformIO通过插件无缝集成到CLion中,可自动管理工具链(如SDCC编译器、stcgal烧录工具)。用户无需手动配置环境变量或编写复杂的Makefile,只需在ini中定义目标硬件(如platform = intel_mcs51)即可一键编译、烧录
- AI插件支持, Github Copilot、Lingma等AI助手工具,方便快速生成代码,或即时解决代码中存在的问题。
- 跨平台开发体验
- 支持多操作系统(Windows/macOS/Linux),避免Keil等传统工具仅限Windows的局限
- 版本控制友好:CLion原生支持Git,结合PlatformIO的依赖管理,便于团队协作和代码复用
- 工具链集成与管理
- 依赖库自动处理。PlatformIO提供丰富的开源库支持(如传感器驱动、通信协议),通过lib_deps字段即可引入。。
- 灵活配置硬件参数。在ini中可定义芯片型号、时钟频率、烧录接口等。避免传统开发中频繁修改编译器参数的繁琐
- 解决传统开发痛点
- 替代Keil的局限。Keil虽成熟易用,但收费高昂、仅限Windows,且封闭性强。CLion+PlatformIO提供免费、开源的替代方案,尤其适合学生和小型项目
- 简化SDCC手动操作。传统SDCC开发需手动执行命令(如sdcc main.c -o out.ihx),CLion+PlatformIO自动化此流程,并支持编译错误直接定位到源码行
使用Clion的挑战
- 调试功能较弱。51单片机缺乏标准的硬件调试接口(如SWD/JTAG),CLion无法直接在线调试。通常需依赖日志输出或模拟器(如Proteus)辅助
- 配置复杂度较高。需熟悉PlatformIO的配置语法,且部分冷门芯片需手动编写板级支持文件(BSP)。初学者可能需参考社区教程
- 资源占用较大。相比Keil或纯文本编辑器(如VSCode),CLion内存占用更高,老旧电脑可能运行缓慢
开发环境的配置
第一步:安装CLion
下载链接:https://www.jetbrains.com/clion/
第二步:安装Clion插件PlatformIO
安装完CLion后需要再安装PlatformIO插件。PlatformIO 是一个开源的跨平台物联网(IoT)开发环境,专注于嵌入式系统和微控制器开发。它集成了代码编辑、编译、调试、库管理和固件烧录等功能,支持 650+种开发板 和 40+种框架(如Arduino、ESP-IDF、STM32Cube、Raspberry Pi Pico等)
Platformio Plus,非必须,不需要安装。
第三步:安装PlatformIO Core
安装完PlatformIO以后并不能直接使用,好需要安装PlatformIO Core库。
- 先安装Python环境(如果电脑上已有可跳过)https://www.python.org/
- 再下载Python的安装程序:https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py
- 再打开命令行(cmd 或终端),进入脚本所在的目录。运行python get-platformio.py
- 执行完后会出现如下提示,按照要求配置环境变量即可:
PlatformIO Core has been successfully installed into an isolated environment `C:\Users\biaod\.platformio\penv`! The full path to `platformio.exe` is `C:\Users\biaod\.platformio\penv\Scripts\platformio.exe` If you need an access to `platformio.exe` from other applications, please install Shell Commands (add PlatformIO Core binary directory `C:\Users\biaod\.platformio\penv\Scripts` to the system environment PATH variable): See https://docs.platformio.org/page/installation.html#install-shell-commands
第四步:配置编译器
第一次创建platformio项目后,会出现如下报错:
未知的编译器可执行文件: ‘C:\Users\biaod\.platformio\packages\toolchain-sdcc\bin\sdcc.exe’
这个应该是Clion默认使用的是CMake导致的?
解决方案:在Clion中配置自定义编译器:
新建custom_compiler.yaml
内容为:
compilers: - description: SDCC for 8051 match-compiler-exe: "(.*/)?sdcc(.exe)?" match-args: -mmcs51 match-language: C include-dirs: - ${project-path}/include - ${compiler-exe-dir}/../include/mcs51 - ${compiler-exe-dir}/../include - ${compiler-exe-dir}/../non-free/include defines-text: " #define __SDCC_USE_NON_FREE 1 #define __SDCC_MCS51 1 #define __STC8G1K08A 1 #define __8051__ 1 #define __STDC__ 1 #define __STDC_ISO_10646__ 201409L #define __STDC_UTF_16__ 1 #define __STDC_UTF_32__ 1 #define __SDCC 4_4_0 #define __SDCC_REVISION 14620 #define __SDCC_VERSION_MAJOR 4 #define __SDCC_VERSION_MINOR 4 #define __SDCC_VERSION_PATCH 0 #define __interrupt(x) #define __using(x) #define __code #define __at(x) #define __sfr #define __sfr16 #define __sfr32 #define __sbit #define __xdata "
并在设置里进行选择创建的custom_compiler.yaml文件。
其他优化:
- 在ini文件中新增配置项:platform_packages = toolchain-sdcc