You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行C++项目时出现'AvailabilityInternalLegacy.h: No such file or directory'错误

问题:CLion中使用Homebrew GCC编译C++项目时出现AvailabilityInternalLegacy.h缺失错误

问题背景

  • 环境:CLion + Homebrew安装的gcc 13.2.0(g++-13),CMake、Ninja工具链采用默认配置
  • 项目此前运行正常,安装其他编译器后配置被打乱,出现编译失败
  • 已尝试操作:重装gcc、重新链接gcc、执行brew cleanup gcc,均未解决问题
  • 拒绝临时方案:不想手动添加AvailabilityInternalLegacy.h文件,希望恢复原有正常配置

相关细节

编译时wchar.h的查找路径从Homebrew目录转向了Apple CommandLineTools,相关代码片段:

#if _GLIBCXX_HAVE_WCHAR_H
#include <wchar.h>
#endif

对应的宏定义位于/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/aarch64-apple-darwin23/bits/c++config.h:

/* Define to 1 if you have the <wchar.h> header file. */
#define _GLIBCXX_HAVE_WCHAR_H 1

编译命令及错误日志

/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/cmake --build /Users/USERNAME/CLionProjects/Project/cmake-build-debug --target file -j 8
[1/2] Building CXX object CMakeFiles/file.dir/file.cpp.o
FAILED: CMakeFiles/file.dir/file.cpp.o 
/opt/homebrew/bin/g++-13   -DLOCAL -g -std=gnu++14 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -fdiagnostics-color=always -MD -MT CMakeFiles/file.dir/file.cpp.o -MF CMakeFiles/file.dir/file.cpp.o.d -o CMakeFiles/file.dir/file.cpp.o -c /Users/USERNAME/CLionProjects/Project/file.cpp
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/Availability.h:166,
                 from /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/wchar.h:72,
                 from /opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/cwchar:44,
                 from /opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/bits/postypes.h:40,
                 from /opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/iosfwd:42,
                 from /opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/ios:40,
                 from /opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/ostream:40,
                 from /opt/homebrew/Cellar/gcc/13.2.0/include/c++/13/iostream:41,
                 from /Users/USERNAME/CLionProjects/Project/file.cpp:5:
/opt/homebrew/Cellar/gcc/13.2.0/lib/gcc/current/gcc/aarch64-apple-darwin23/13/include-fixed/AvailabilityInternal.h:185:10: fatal error: AvailabilityInternalLegacy.h: No such file or directory
  185 | #include <AvailabilityInternalLegacy.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.

解决方案建议

1. 修复CommandLineTools头文件关联

  • 重置CommandLineTools路径:sudo xcode-select --reset
  • 重新安装CommandLineTools:xcode-select --install,按提示完成安装

2. 强制GCC优先使用自身头文件

在项目的CMakeLists.txt中添加编译选项,指定GCC自身的头文件路径:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /opt/homebrew/Cellar/gcc/13.2.0/lib/gcc/current/gcc/aarch64-apple-darwin23/13/include-fixed")

3. 检查并修复CLion工具链配置

  • 打开CLion的Settings -> Build, Execution, Deployment -> Toolchains
  • 确认当前工具链使用的是Homebrew的gcc(路径为/opt/homebrew/bin/g++-13),而非Xcode自带的clang
  • 若存在多个工具链,将Homebrew GCC设置为默认工具链

4. 重新生成CMake缓存

  • 在CLion中点击File -> Reload CMake Project
  • 或直接删除cmake-build-debug目录后,重新执行构建操作

内容的提问来源于stack exchange,提问作者MangoPizza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:31:23