在Ubuntu编译libc++遇类型特性相关错误,请求技术支援
编译libc++时遇到类型特性相关编译错误
在Ubuntu机器上按照libc++官方文档的默认编译步骤操作,编译过程中出现一系列类型特性相关的编译错误,具体错误信息如下:
from ~/projects/libcxx/llvm-project/libcxxabi/src/stdlib_exception.cpp:9: ~/projects/libcxx/llvm-project/build/include/c++/v1/__type_traits/is_trivially_destructible.h:36:4: error: #error is_trivially_destructible is not implemented 36 | # error is_trivially_destructible is not implemented | ^~~~~ In file included from ~/projects/libcxx/llvm-project/build/include/c++/v1/new:95, from ~/projects/libcxx/llvm-project/libcxxabi/src/stdlib_exception.cpp:9: ~/projects/libcxx/llvm-project/build/include/c++/v1/__type_traits/is_same.h:22:53: error: there are no arguments to ‘__is_same’ that depend on a template parameter, so a declaration of ‘__is_same’ must be available [-fpermissive] 22 | struct _LIBCPP_TEMPLATE_VIS is_same : _BoolConstant<__is_same(_Tp, _Up)> {}; | ^~~~~~~~~ ~/projects/libcxx/llvm-project/build/include/c++/v1/__type_traits/is_same.h:22:53: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated) ~/projects/libcxx/llvm-project/build/include/c++/v1/__type_traits/is_same.h:22:72: error: template argument 1 is invalid 22 | struct _LIBCPP_TEMPLATE_VIS is_same : _BoolConstant<__is_same(_Tp, _Up)> {}; | ^ ~/projects/libcxx/llvm-project/build/include/c++/v1/__type_traits/is_same.h:26:48: error: expected primary-expression before ‘,’ token 26 | inline constexpr bool is_same_v = __is_same(_Tp, _Up); | ^ ~/projects/libcxx/llvm-project/build/include/c++/v1/__type_traits/is_same.h:26:53: error: expected primary-expression before ‘)’ token 26 | inline constexpr bool is_same_v = __is_same(_Tp, _Up);
解决方法
1. 升级编译器版本
这类错误大多因使用的GCC版本过低,不支持libc++依赖的内置类型特性函数(如__is_same、__is_trivially_destructible)。Ubuntu系统可通过以下命令升级到符合要求的GCC版本:
sudo apt update sudo apt install gcc-10 g++-10 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100
2. 显式指定编译器重新配置CMake
进入构建目录,清理原有配置后,指定升级后的编译器重新生成构建文件:
cd ~/projects/libcxx/llvm-project/build rm -rf ./* cmake -G Ninja ../llvm \ -DCMAKE_C_COMPILER=gcc-10 \ -DCMAKE_CXX_COMPILER=g++-10 \ -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi"
3. 确保LLVM仓库完整
检查并补全llvm-project的子模块,避免因文件缺失导致编译错误:
cd ~/projects/libcxx/llvm-project git submodule update --init --recursive
4. 切换到Clang编译器编译
libc++原生适配Clang,使用Clang可规避多数GCC兼容性问题。先安装Clang:
sudo apt install clang-14 lldb-14 lld-14 sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 100 sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-14 100
再重新配置CMake:
cd ~/projects/libcxx/llvm-project/build rm -rf ./* cmake -G Ninja ../llvm \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi"
内容的提问来源于stack exchange,提问作者DDG
相关产品推荐
相关产品推荐

