cmake 提示gcc或交叉编译 broken

本文解决了一个常见的CMake配置问题,即CMake报告GCC无法编译简单的测试程序。通过设置CMake以强制使用特定版本的GCC,并确保环境变量正确指向编译器路径,可以解决这一问题。

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

cmake reports gcc is broken. Where do I start?

-- Check for working C compiler: /usr/bin/gcc -- broken

CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message): The C compiler "/usr/bin/gcc" is not able to compile a simple test program.

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Could not determine Eclipse version, assuming at least 3.6 (Helios). Adjust CMAKE_ECLIPSE_VERSION if this is wrong.
-- Check for working C compiler: /usr/bin/gcc-4.6
-- Check for working C compiler: /usr/bin/gcc-4.6 --  broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
The C compiler "/usr/bin/gcc-4.6" is not able to compile a simple testprogram.


解决


check this Link. i had the same problem. i think the problem is that you need more parameters to compile (like linker-script, flags ...). change your CMakeLists like this and try it again:

INCLUDE(CMakeForceCompiler) 
CMAKE_FORCE_C_COMPILER(gcc GNU)
CMAKE_FORCE_CXX_COMPILER(g++ GNU)

do not forget to set the path to the compiler in your enviroment vars.

参考

http://stackoverflow.com/questions/16594053/cmake-reports-gcc-is-broken-where-do-i-start
D:\STM32CUBECLT\CMake\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=D:/STM32CUBECLT/Ninja/bin/ninja.exe -DCMAKE_C_COMPILER=D:/STM32CUBECLT/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER=D:/STM32CUBECLT/GNU-tools-for-STM32/bin/arm-none-eabi-c++.exe -G Ninja -S D:\el\1231123213\MDK-ARM\stm32\xuexi -B D:\el\1231123213\MDK-ARM\stm32\xuexi\cmake-build-debug -- The C compiler identification is GNU 13.3.1 -- The CXX compiler identification is GNU 13.3.1 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: D:/STM32CUBECLT/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe -- Check for working C compiler: D:/STM32CUBECLT/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe - broken CMake Error at D:/STM32CUBECLT/CMake/share/cmake-3.28/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "D:/STM32CUBECLT/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe" is not able to compile a simple test program. It fails with the following output: Change Dir: 'D:/el/1231123213/MDK-ARM/stm32/xuexi/cmake-build-debug/CMakeFiles/CMakeScratch/TryCompile-lpeiv3' Run Build Command(s): D:/STM32CUBECLT/Ninja/bin/ninja.exe -v cmTC_2c52d [1/2] D:\STM32CUBECLT\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe -std=gnu11 -fdiagnostics-color=always -o CMakeFiles/cmTC_2c52d.dir/testCCompiler.c.obj -c D:/el/1231123213/MDK-ARM/stm32/xuexi/cmake-build-debug/CMakeFiles/CMakeScratch/TryCompile-lpeiv3/testCCompiler.c [2/2] C:\WINDOWS\system32\cmd.exe /C "cd . && D:\STM32CUBECLT\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe CMakeFiles/cmTC_2c52d.dir/testCCompiler.c.obj -o cmTC_2c52d.exe -Wl,--out-implib,libcmTC_2c52d.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ." FAILED: cmTC_2c52d.exe C:\WINDOWS\system32\cmd.exe /C "cd . && D:\STM32CUBECLT\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe CMakeFiles/cmTC_2c52d.dir/testCCompiler.c.obj -o cmTC_2c52d.exe -Wl,--out-implib,libcmTC_2c52d.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ." Cannot create temporary file in C:\Users\张宇鹏\AppData\Local\Temp\: No such file or directory arm-none-eabi-gcc.exe: internal compiler error: Aborted signal terminated program collect2 Please submit a full bug report, with preprocessed source (by using -freport-bug). See <https://gcc.gnu.org/bugs/> for instructions. ninja: build stopped: subcommand failed. CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:28 (project) -- Configuring incomplete, errors occurred! [已完成]
最新发布
07-14
<think>我们正在处理一个CMake配置失败的问题,错误信息为: arm-none-eabi-gcc: Cannot create temporary file in Temp directory, internal compiler error Aborted 根据错误信息,问题出在编译器无法在临时目录中创建临时文件。这通常是由于权限问题磁盘空间不足导致的。 参考引用[1]中提到了通过设置环境变量来指定编译器路径,但这里的问题不是找不到编译器,而是编译器在写临时文件时出错。 可能的解决方案: 1. 检查临时目录的权限:确保当前用户对临时目录有写入权限。 2. 检查磁盘空间:使用`df -h`命令查看磁盘空间是否充足。 3. 指定临时目录:通过设置环境变量`TMPDIR`来更改临时目录的位置。 另外,参考引用[3]中出现了类似的问题,但错误信息略有不同,它提示的是“No such file or directory”,而我们的问题是“Cannot create temporary file”。不过,它们都涉及到文件路径的问题。 步骤: 1. 确定当前临时目录的位置: 在Linux中,临时目录通常是`/tmp`,在Windows中通常是`%TEMP%``%TMP%`。 2. 检查临时目录权限: 在Linux下,可以运行: ls -ld /tmp 确保权限为drwxrwxrwt。 3. 尝试更改临时目录: 如果当前临时目录不可写,可以尝试设置一个不同的临时目录。 例如,在Linux中: export TMPDIR=/path/to/writable/dir 在Windows中(CMD): set TMP=C:\path\to\writable\dir set TEMP=C:\path\to\writable\dir 者在PowerShell中: $env:TMP = "C:\path\to\writable\dir" $env:TEMP = "C:\path\to\writable\dir" 4. 在CMake配置时指定环境变量: 由于我们是在配置交叉编译,可以在调用CMake之前设置环境变量。 5. 如果是在VSCode中,可以在CMake配置预设(preset)中设置环境变量,者在`CMakePresets.json`中通过`environment`字段设置。 修改预设文件示例(在configurePresets中添加环境变量): ```json { "version": 3, "configurePresets": [ { "name": "Debug", "inherits": "default", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" }, "environment": { "TMPDIR": "/path/to/your/tmp" // 对于Linux // 者在Windows中使用: // "TMP": "C:\\path\\to\\your\\tmp", // "TEMP": "C:\\path\\to\\your\\tmp" } } ] } ``` 6. 检查磁盘空间: 如果磁盘空间不足,清理磁盘空间。 7. 检查是否有防病毒软件阻止了编译器创建临时文件。 8. 检查临时目录路径是否包含空格特殊字符,这有时也会导致问题(尤其是在Windows上)。 9. 如果使用Docker容器,确保临时目录被正确映射并且有写入权限。 根据错误信息,我们重点解决临时目录的写入问题。 下面我们给出一个完整的解决方案步骤: 步骤1:确定临时目录位置 - Linux: `echo $TMPDIR` `echo $TMP` `echo $TEMP`,如果这些环境变量未设置,则默认使用`/tmp`。 - Windows: 在CMD中执行`echo %TEMP%`。 步骤2:检查该目录的权限和空间 - 权限:尝试在该目录中创建一个文件,例如: touch /tmp/testfile # Linux echo test > %TEMP%\testfile.txt # Windows 如果失败,则说明没有写入权限。 步骤3:如果权限不足,尝试更改临时目录 - 创建一个新的临时目录,并设置环境变量指向它。 步骤4:在CMake配置时使用新的临时目录 - 在运行CMake之前设置环境变量,者在预设中设置。 步骤5:重新运行CMake配置 示例(Linux): ```bash mkdir ~/mytmp chmod 700 ~/mytmp export TMPDIR=~/mytmp cmake --preset Debug ``` 示例(Windows PowerShell): ```powershell mkdir C:\mytmp $env:TEMP = "C:\mytmp" $env:TMP = "C:\mytmp" cmake --preset Debug ``` 如果问题解决,可以将这些环境变量设置添加到你的shell配置文件中(如.bashrcprofile)者系统环境变量中。 另外,如果问题仍然存在,可以尝试使用编译器的`-pipe`选项(如果支持)来避免使用临时文件。但是,arm-none-eabi-gcc可能不支持这个选项,者即使支持,也可能在某些阶段需要临时文件。 在CMake中,我们可以尝试在工具链文件中添加编译选项: 在`gcc-arm-none-eabi.cmake`工具链文件中,添加: ```cmake set(CMAKE_C_FLAGS_INIT "-pipe") set(CMAKE_CXX_FLAGS_INIT "-pipe") ``` 但请注意,这并不能保证解决所有临时文件问题,因为编译器在某些阶段(如链接)可能仍然需要临时文件。 因此,我们优先解决临时目录的权限和路径问题。 最后,如果问题依旧,可以尝试更新重新安装编译器,以排除编译器本身的问题。 总结:本问题的核心是临时目录的访问问题,通过设置正确的临时目录路径并确保有读写权限,可以解决。 下面我们给出一个完整的工具链文件示例,其中包含设置`-pipe`选项(可选):</think>### 🔧 解决 CMake 配置失败:arm-none-eabi-gcc 无法创建临时文件问题 #### 🔍 问题分析 当 CMake 配置 STM32 项目时出现错误 `arm-none-eabi-gcc: Cannot create temporary file in Temp directory`,通常由以下原因导致: 1. **临时目录权限问题**:编译器无法在系统临时目录创建文件 2. **磁盘空间不足**:临时目录所在磁盘空间耗尽 3. **路径包含特殊字符**:临时目录路径包含空格特殊符号 4. **防病毒软件拦截**:安全软件阻止编译器创建临时文件 #### 🛠️ 完整解决步骤 ##### 1. **检查临时目录权限** ```bash # Linux/macOS ls -ld /tmp # 确保权限为 drwxrwxrwt # Windows echo %TEMP% # 检查路径是否存在且可写 ``` ##### 2. **显式设置临时目录环境变量** 在 CMake 预设中配置环境变量(推荐方法): ```json { "version": 3, "configurePresets": [ { "name": "Debug", "inherits": "default", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" }, "environment": { "TMPDIR": "/path/to/custom/tmp", // Linux/macOS "TEMP": "C:\\Custom\\Temp" // Windows } } ] } ``` 在终端中设置: ```bash # Linux/macOS export TMPDIR=~/custom_tmp # Windows set TEMP=C:\Custom\Temp ``` ##### 3. **验证磁盘空间** ```bash # Linux/macOS df -h /tmp # Windows dir %TEMP% ``` ##### 4. **配置编译器路径(引用[1][2])** 确保工具链文件正确指向编译器: ```cmake # 在 gcc-arm-none-eabi.cmake 中设置 set(CMAKE_C_COMPILER "/usr/local/bin/arm-none-eabi-gcc") set(CMAKE_CXX_COMPILER "/usr/local/bin/arm-none-eabi-g++") ``` ##### 5. **处理路径特殊字符** 如果路径包含空格: ```cmake # 在工具链文件中使用引号 set(CMAKE_C_COMPILER "C:/Program Files (x86)/GNU Arm Embedded Toolchain/bin/arm-none-eabi-gcc.exe") ``` ##### 6. **禁用防病毒软件(临时)** 测试时暂时禁用安全软件,特别是当错误包含 `internal compiler error Aborted` 时 ##### 7. **完整工具链文件示例** ```cmake # gcc-arm-none-eabi.cmake set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_PROCESSOR ARM) # 关键:显式指定编译器路径 set(CMAKE_C_COMPILER "/usr/local/bin/arm-none-eabi-gcc") set(CMAKE_CXX_COMPILER "/usr/local/bin/arm-none-eabi-g++") # 设置临时目录(可选) set(ENV{TMPDIR} "/project_temp") # 嵌入式开发必需标志 set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-m4 -mthumb -specs=nosys.specs") set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--gc-sections") ``` #### ✅ 验证解决方案 ```bash # 清理旧配置 rm -rf build/ # 使用新预设配置 cmake --preset Debug # 检查临时文件生成 ls /path/to/custom/tmp/*.tmp ``` #### ⚠️ 常见陷阱 1. **Windows 路径分隔符**:使用 `/` 双反斜杠 `\\` ```cmake # 错误 set(CMAKE_C_COMPILER "C:\Program Files\arm-gcc\bin\arm-none-eabi-gcc.exe") # 正确 set(CMAKE_C_COMPILER "C:/Program Files/arm-gcc/bin/arm-none-eabi-gcc.exe") ``` 2. **VSCode 配置同步**(引用[2]): - 更新 `c_cpp_properties.json` 中的 `compilerPath` - 重启 VSCode 使配置生效 3. **工具链文件权限**: ```bash chmod +x cmake/gcc-arm-none-eabi.cmake ``` #### 📊 问题排查流程图 ```mermaid graph TD A[配置失败] --> B{检查临时目录} B -->|权限不足| C[设置自定义TEMP变量] B -->|空间不足| D[清理磁盘空间] C --> E[更新CMake预设] D --> E E --> F[验证工具链路径] F -->|路径错误| G[修正编译器路径] F -->|路径正确| H[检查特殊字符] H -->|有空格| I[添加引号包裹路径] H -->|无问题| J[测试防禁用软件] ``` #### 💡 预防措施 1. **固定临时目录**:在项目根目录创建 `.tmp` 并设置 `TMPDIR=$PWD/.tmp` 2. **工具链版本管理**:使用版本管理工具(如 asdf)确保编译器一致性 3. **容器化开发**:使用 Docker 避免环境差异 ```Dockerfile FROM ubuntu:22.04 RUN apt-get update && apt-get install -y gcc-arm-none-eabi ENV TMPDIR=/tmp ``` --- ### ❓ 相关问题 1. 如何在 CMake 中为不同目标设置不同的编译器选项? 2. 如何解决 arm-none-eabi-gcc 编译时的 "missing include" 错误? 3. VSCode 中如何配置 CMake 预设与 STM32CubeIDE 协同工作? 4. 如何为 STM32 项目配置链接器脚本和启动文件? 5. CMake 中如何管理嵌入式开发的依赖库(如 HAL 库)?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值