Arduino编译错误解决办法:fork/exec:…\arm-none-eabi-g++.exe: The filename or extension is too long

当使用Arduino IDE编译STM32、ESP32或其他平台的项目时,可能会遇到由于命令行字符数超过Windows 32k限制的错误。这个问题可以通过创建`platform.local.txt`文件并在相应硬件目录下添加特定代码来解决,将对象文件路径写入文本文件并在链接阶段引用,从而避免命令行长度超出限制。该方法适用于Arduino、Adafruit、esp32和STM32平台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译错误解决办法:fork/exec:…\arm-none-eabi-g++.exe: The filename or extension is too long

解决方案来自Edge Impulse

当使用Arduino编译STM或Arduino文件时,当要编译的目标文件列表超过命令行中的Windows最大字符数(32k)时,通常会引发此错误。

如果使用的2.0版本以下IDE版本,可参考以下方法:

如果是Arduino则使用前两种方法即可,Adafruit、esp32、STM32参照后面的方法

1.在Arduino mbed目录下,即:C:\Users\(自己的用户名)\AppData\Local\Arduino15\packages\arduino\hardware\mbed\1.1.4\ (版本号可能不同)
创建platform.local.txt 并粘贴以下代码 :

## Customized platform.local.txt to compile for mbed targets under Windows
## This recipe works around the Windows limit of 32k characters in a cmd line by calling .o files from a text file during linking
## Install: put file under your mbed sub-directory - ie: C:\Users\MYUSER\AppData\Local\Arduino15\packages\arduino\hardware\mbed\1.1.4
## You can also find the local arduino folder in the IDE under File -> Preferences -> see preferences.txt location in the bottom part of the window

## hooks

# create a txt file with all object file paths from libraries and sketch folders (core\variant.cpp.o needed too for linking)
recipe.hooks.linking.prelink.1.pattern=cmd /c dir /b /s {build.path}\sketch\*.o > {build.path}\obj_files_tmp.txt
recipe.hooks.linking.prelink.2.pattern=cmd /c "dir /b /s {build.path}\libraries\*.o >> {build.path}\obj_files_tmp.txt 2>nul & exit 0"
recipe.hooks.linking.prelink.3.pattern=cmd /c echo "{build.path}\core\variant.cpp.o" >> {build.path}\obj_files_tmp.txt

# replace \ by \\ in file paths (otherwise escaped by linker) and save in new txt file
recipe.hooks.linking.prelink.4.pattern=cmd /v /c "@echo off && for /f %a in ({build.path}\obj_files_tmp.txt) do (set line=%a && set line=!line:\=\\! && echo !line! >> {build.path}\obj_files.txt)"

# delete txt file after linking
recipe.hooks.linking.postlink.1.pattern=cmd /c del {build.path}\obj_files.txt


## modify compile patterns (@{build.path}\obj_files.txt)

recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}"  "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "@{compiler.mbed.ldflags}" "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" @{build.path}\obj_files.txt -Wl,--whole-archive "{build.path}/{archive_file}" {compiler.mbed} -Wl,--no-whole-archive -Wl,--start-group {compiler.mbed.extra_ldflags} {compiler.libraries.ldflags} -Wl,--end-group

如图所示:在这里插入图片描述

2.在Arduino samd目录下,即:C:\Users\(自己的用户名)\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.1.4
创建platform.local.txt 并粘贴以下代码 :

## Customized platform.local.txt to compile for SAMD21 targets under Windows
## This recipe works around the Windows limit of 32k characters in a cmd line by calling .o files from a text file during linking
## Install: put file under your mbed sub-directory - ie: C:\Users\MYUSER\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.9
## You can also find the local arduino folder in the IDE under File -> Preferences -> see preferences.txt location in the bottom part of the window

## hooks

# create a txt file with all object file paths from libraries and sketch folders (core\variant.cpp.o needed too for linking)
recipe.hooks.linking.prelink.1.pattern=cmd /c dir /b /s {build.path}\sketch\*.o > {build.path}\obj_files_tmp.txt
recipe.hooks.linking.prelink.2.pattern=cmd /c "dir /b /s {build.path}\libraries\*.o >> {build.path}\obj_files_tmp.txt 2>nul & exit 0"
recipe.hooks.linking.prelink.3.pattern=cmd /c echo "{build.path}\core\variant.cpp.o" >> {build.path}\obj_files_tmp.txt

# replace \ by \\ in file paths (otherwise escaped by linker) and save in new txt file
recipe.hooks.linking.prelink.4.pattern=cmd /v /c "@echo off && for /f %a in ({build.path}\obj_files_tmp.txt) do (set line=%a && set line=!line:\=\\! && echo !line! >> {build.path}\obj_files.txt)"

# delete txt file after linking
recipe.hooks.linking.postlink.1.pattern=cmd /c del {build.path}\obj_files.txt


## modify compile patterns (@{build.path}\obj_files.txt)
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}"  "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" @{build.path}\obj_files.txt {compiler.libraries.ldflags} -Wl,--start-group {compiler.arm.cmsis.ldflags} -lm "{build.path}/{archive_file}" -Wl,--end-group

3.如果是Adafruit,则
在Arduino Adafruit samd目录下,即:C:\Users\(自己的用户名)\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.6.3
创建platform.local.txt 并粘贴以下代码 :

## Customized platform.local.txt to compile for Adafruit SAMD51 targets under Windows
## This recipe works around the Windows limit of 32k characters in a cmd line by calling .o files from a text file during linking
## Install: put file under your mbed sub-directory - ie: C:\Users\MYUSER\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.9
## You can also find the local arduino folder in the IDE under File -> Preferences -> see preferences.txt location in the bottom part of the window

## hooks

# create a txt file with all object file paths from libraries and sketch folders (core\variant.cpp.o needed too for linking)
recipe.hooks.linking.prelink.1.pattern=cmd /c dir /b /s {build.path}\sketch\*.o > {build.path}\obj_files_tmp.txt
recipe.hooks.linking.prelink.2.pattern=cmd /c "dir /b /s {build.path}\libraries\*.o >> {build.path}\obj_files_tmp.txt 2>nul & exit 0"
recipe.hooks.linking.prelink.3.pattern=cmd /c echo "{build.path}\core\variant.cpp.o" >> {build.path}\obj_files_tmp.txt

# replace \ by \\ in file paths (otherwise escaped by linker) and save in new txt file
recipe.hooks.linking.prelink.4.pattern=cmd /v /c "@echo off && for /f %a in ({build.path}\obj_files_tmp.txt) do (set line=%a && set line=!line:\=\\! && echo !line! >> {build.path}\obj_files.txt)"

# delete txt file after linking
recipe.hooks.linking.postlink.1.pattern=cmd /c del {build.path}\obj_files.txt


## modify compile patterns (@{build.path}\obj_files.txt)
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}"  "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" @{build.path}\obj_files.txt {compiler.libraries.ldflags} -Wl,--start-group {compiler.arm.cmsis.ldflags} "-L{build.variant.path}" -lm "{build.path}/{archive_file}" -Wl,--end-group

4.如果是ESP32,则在Arduino ESP32目录下,即:C:\Users\(自己的用户名)\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4
创建platform.local.txt 并粘贴以下代码 :


## Customized platform.local.txt to compile for ESP32 targets under Windows
## This recipe works around the Windows limit of 32k characters in a cmd line, creating an archive with sketch and libraries object files before linking
## Install: put file under your esp32 sub-directory - ie:C:\Users\MYUSER\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4
## You can also find the local arduino folder in the IDE under File -> Preferences -> see preferences.txt location in the bottom part of the window

## hooks

# create a txt file with all object file paths from libraries and sketch folders
recipe.hooks.linking.prelink.1.pattern=cmd /c dir /b /s {build.path}\sketch\*.o > {build.path}\obj_files_tmp.txt
recipe.hooks.linking.prelink.2.pattern=cmd /c "dir /b /s {build.path}\libraries\*.o >> {build.path}\obj_files_tmp.txt 2>nul & exit 0"

# replace \ by \\ in file paths (otherwise escaped by linker) and save in new txt file
recipe.hooks.linking.prelink.3.pattern=cmd /v /c "@echo off && for /f %a in ({build.path}\obj_files_tmp.txt) do (set line=%a && set line=!line:\=\\! && echo !line! >> {build.path}\obj_files.txt)"

# save object files in archive
recipe.hooks.linking.prelink.4.pattern=cmd /v /c "@echo off && for /f %a in ({build.path}\obj_files.txt) do ({compiler.path}{compiler.ar.cmd} {compiler.ar.flags} {compiler.ar.extra_flags} {build.path}\custom_lib.a %a)"

# delete txt file and archive after linking
recipe.hooks.linking.postlink.1.pattern=cmd /c del {build.path}\obj_files.txt
recipe.hooks.linking.postlink.2.pattern=cmd /c del {build.path}\custom_lib.a


## modify compile patterns (use custom_lib.a instead of object_files)
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -Wl,--start-group "{build.path}\custom_lib.a" "{archive_file_path}" {compiler.c.elf.libs} -Wl,--end-group -Wl,-EL -o "{build.path}/{build.project_name}.elf"

5.如果是STM32,则在Arduino STM32目录下,即:C:\Users\(自己的用户名)\AppData\Local\Arduino15\packages\esp32\hardware\stm32\1.9.0
创建platform.local.txt 并粘贴以下代码 :

## Customized platform.local.txt to compile for STM32 targets under Windows
## This recipe works around the Windows limit of 32k characters in a cmd line by calling .o files from a text file during linking
## Install: put file under your mbed sub-directory - ie: C:\Users\MYUSER\AppData\Local\Arduino15\packages\arduino\hardware\stm32\1.9.0
## You can also find the local arduino folder in the IDE under File -> Preferences -> see preferences.txt location in the bottom part of the window

## hooks

# create a txt file with all object file paths from libraries and sketch folders
recipe.hooks.linking.prelink.1.pattern=cmd /c dir /b /s {build.path}\sketch\*.o > {build.path}\obj_files_tmp.txt
recipe.hooks.linking.prelink.2.pattern=cmd /c "dir /b /s {build.path}\libraries\*.o >> {build.path}\obj_files_tmp.txt 2>nul & exit 0"
recipe.hooks.linking.prelink.3.pattern=cmd /c echo "{build.path}\core\variant.cpp.o" >> {build.path}\obj_files_tmp.txt
recipe.hooks.linking.prelink.4.pattern=cmd /c echo "{build.path}\core\PeripheralPins.c.o" >> {build.path}\obj_files_tmp.txt

# replace \ by \\ in file paths (otherwise escaped by linker) and save in new txt file
recipe.hooks.linking.prelink.5.pattern=cmd /v /c "@echo off && for /f %a in ({build.path}\obj_files_tmp.txt) do (set line=%a && set line=!line:\=\\! && echo !line! >> {build.path}\obj_files.txt)"

# delete txt file after linking
recipe.hooks.linking.postlink.1.pattern=cmd /c del {build.path}\obj_files.txt


## modify compile patterns (@{build.path}\obj_files.txt)
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} "-Wl,--default-script={build.variant.path}/{build.ldscript}" "-Wl,--script={build.system.path}/ldscript.ld" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} {compiler.ldflags} {compiler.arm.cmsis.ldflags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group @{build.path}\obj_files.txt {compiler.libraries.ldflags} "{archive_file_path}" -lc -Wl,--end-group -lm -lgcc -lstdc++

进行以上操作后,测试编译成功
在这里插入图片描述

如果您觉得文章对您有帮助,欢迎转发打赏
在这里插入图片描述

### 关于ESP32开发板编译时缺少`xtensa-esp32-elf-g++`工具的问题 当遇到 `exec: "/bin/xtensa-esp32-elf-g++": file does not exist` 的错误时,这通常表明交叉编译器未正确安装或路径配置不正确。以下是可能的原因及解决办法: #### 可能原因 1. **交叉编译工具链缺失** 如果系统中不存在 `xtensa-esp32-elf-g++` 工具,则说明所需的交叉编译工具链尚未安装。 2. **环境变量PATH设置不当** 即使已安装了工具链,但如果其目录未被加入到系统的 `PATH` 环境变量中,也会导致此问题。 3. **文件权限不足** 安装后的工具可能存在权限问题,无法被执行。 --- #### 解决方法 1. **确认工具链是否已安装** 需要验证是否存在 `xtensa-esp32-elf-g++` 文件及其所在路径。可以通过以下命令查找: ```bash find / -name "xtensa-esp32-elf-g++" 2>/dev/null ``` 2. **更新环境变量PATH** 将工具链所在的路径添加至 `PATH` 中。假设工具链位于 `/opt/espressif/xtensa-esp32-elf/bin`,可以执行如下操作: ```bash export PATH=$PATH:/opt/espressif/xtensa-esp32-elf/bin ``` 或将其永久化写入 `.bashrc` 文件中: ```bash echo 'export PATH=$PATH:/opt/espressif/xtensa-esp32-elf/bin' >> ~/.bashrc source ~/.bashrc ``` 3. **重新安装工具链** 若上述步骤未能解决问题,建议卸载并重新安装工具链。可以从 Espressif 提供的官方资源获取最新版本[^3]。 4. **检查文件权限** 使用以下命令确保目标文件具有可执行权限: ```bash chmod +x /path/to/xtensa-esp32-elf-g++ ``` --- ### 下载 Arduino ESP32 包失败的解决方案 如果尝试通过 Arduino IDE 下载 `package_esp32_dev_index.json` 和 `package_esp32_index.json` 失败,可能是由于网络连接不稳定、URL 错误或其他依赖项丢失所致。 #### 常见原因 1. **网络代理问题** 用户所在地可能因防火墙等原因阻止访问默认 URL。 2. **JSON 路径失效** 默认 JSON 地址可能已经更改或不可用。 3. **本地缓存损坏** Arduino IDE 的内部缓存可能导致下载中断。 --- #### 解决方法 1. **手动指定自定义 JSON URL** 打开 Arduino IDE 设置,在 `Preferences -> Additional Boards Manager URLs` 字段中输入最新的 ESP32 板支持包地址。例如: ``` https://dl.espressif.com/dl/package_esp32_index.json ``` 2. **离线安装支持包** 访问 [Espressif 官方网站](https://www.espressif.com/) 并下载对应的支持包压缩文件。解压后放置在 Arduino IDE 的硬件库目录下(通常是 `~/.arduino15/packages/esp32/hardware/esp32/<version>`),再重启 IDE[^4]。 3. **清理缓存** 删除旧版缓存数据以避免冲突。具体位置取决于操作系统: - Windows: `%APPDATA%\Local\Arduino15` - macOS/Linux: `~/.arduino15` 4. **切换镜像源** 对于国内用户,推荐使用清华大学开源软件镜像站提供的替代链接: ``` http://mirror.tuna.tsinghua.edu.cn/arduino/esp/ ``` --- ### 示例代码:验证工具链状态 以下是一个简单的测试脚本,用于检测工具链是否正常工作: ```cpp #include <iostream> int main() { std::cout << "Toolchain test successful!" << std::endl; return 0; } ``` 保存为 `test.cpp` 后运行以下命令进行编译: ```bash xtensa-esp32-elf-g++ -o test test.cpp && ./test ``` --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值