功能: 自动检测并升级C++编译器版本
问题背景: - better-sqlite3 v11+ 需要C++20标准支持 - C++20需要g++ 10或更高版本 - Ubuntu 20.04等老系统默认g++ 9.x,不支持C++20 - 导致WSL2用户编译失败: "unrecognized command line option '-std=c++20'" 实现内容: 1. 新增check_cpp_compiler()函数 - 检测g++是否已安装 - 获取当前g++版本号 - 如果版本<10,自动升级 2. 支持多个Linux发行版: - Ubuntu/Debian: 添加toolchain PPA,安装g++-11 - CentOS 7: 安装devtoolset-11 - CentOS 8+: 安装gcc-toolset-11 - OpenSUSE: 安装gcc11-c++ 3. 自动设置为默认编译器: - 使用update-alternatives设置优先级 - CentOS使用source启用gcc-toolset 4. 调用时机: - 安装模式:install_dependencies()末尾 - 更新模式:update_install_dependencies()中 5. 错误处理: - 升级失败时给出明确提示 - 不支持的系统给出手动升级建议 测试场景: - ✅ Ubuntu 20.04 (g++ 9.x → 11.x) - ✅ CentOS 7/8 - ✅ 已有g++ 10+的系统(跳过升级) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
111
install.sh
111
install.sh
@@ -380,6 +380,111 @@ EOF
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
################################################################################
|
################################################################################
|
||||||
|
check_cpp_compiler() {
|
||||||
|
print_step "检查C++编译器版本..."
|
||||||
|
|
||||||
|
# 检查g++是否已安装
|
||||||
|
if ! command -v g++ &> /dev/null; then
|
||||||
|
print_warning "g++未安装,将在依赖安装时自动安装"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 获取g++版本号
|
||||||
|
GXX_VERSION=$(g++ --version | head -n1 | grep -oP '\d+\.\d+\.\d+' | head -1 | cut -d'.' -f1)
|
||||||
|
|
||||||
|
if [[ -z "$GXX_VERSION" ]]; then
|
||||||
|
print_warning "无法检测g++版本,跳过版本检查"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_info "当前g++版本: $GXX_VERSION.x"
|
||||||
|
|
||||||
|
# better-sqlite3 v11+ 需要C++20支持(g++ 10+)
|
||||||
|
if [[ $GXX_VERSION -lt 10 ]]; then
|
||||||
|
print_warning "g++版本过低(需要10+以支持C++20),正在升级..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
case $PKG_MANAGER in
|
||||||
|
apt)
|
||||||
|
# Ubuntu/Debian: 使用toolchain PPA
|
||||||
|
print_info "添加Ubuntu Toolchain PPA..."
|
||||||
|
add-apt-repository ppa:ubuntu-toolchain-r/test -y || {
|
||||||
|
print_error "添加PPA失败"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
apt-get update
|
||||||
|
|
||||||
|
print_info "安装g++-11..."
|
||||||
|
apt-get install -y g++-11 gcc-11 || {
|
||||||
|
print_error "g++-11安装失败"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# 设置为默认编译器
|
||||||
|
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
|
||||||
|
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
|
||||||
|
|
||||||
|
# 验证
|
||||||
|
NEW_VERSION=$(g++ --version | head -n1 | grep -oP '\d+\.\d+\.\d+' | head -1 | cut -d'.' -f1)
|
||||||
|
print_success "g++已升级到版本: $NEW_VERSION.x"
|
||||||
|
;;
|
||||||
|
|
||||||
|
yum|dnf)
|
||||||
|
# CentOS/RHEL: 使用devtoolset或gcc-toolset
|
||||||
|
if [[ "$OS" == "centos" ]] && [[ "$OS_VERSION" == "7" ]]; then
|
||||||
|
# CentOS 7 使用devtoolset-11
|
||||||
|
print_info "安装devtoolset-11..."
|
||||||
|
yum install -y centos-release-scl
|
||||||
|
yum install -y devtoolset-11-gcc devtoolset-11-gcc-c++
|
||||||
|
|
||||||
|
# 启用devtoolset-11
|
||||||
|
echo "source /opt/rh/devtoolset-11/enable" >> /etc/profile
|
||||||
|
source /opt/rh/devtoolset-11/enable
|
||||||
|
|
||||||
|
print_success "devtoolset-11安装完成"
|
||||||
|
else
|
||||||
|
# CentOS 8+ 使用gcc-toolset-11
|
||||||
|
print_info "安装gcc-toolset-11..."
|
||||||
|
$PKG_MANAGER install -y gcc-toolset-11-gcc gcc-toolset-11-gcc-c++
|
||||||
|
|
||||||
|
# 启用gcc-toolset-11
|
||||||
|
echo "source /opt/rh/gcc-toolset-11/enable" >> /etc/profile
|
||||||
|
source /opt/rh/gcc-toolset-11/enable
|
||||||
|
|
||||||
|
print_success "gcc-toolset-11安装完成"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
zypper)
|
||||||
|
# OpenSUSE
|
||||||
|
print_info "升级g++..."
|
||||||
|
zypper install -y gcc11-c++ || {
|
||||||
|
print_error "g++升级失败"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# 设置为默认
|
||||||
|
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
|
||||||
|
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
|
||||||
|
|
||||||
|
print_success "g++已升级"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
print_warning "不支持的包管理器,无法自动升级g++"
|
||||||
|
print_warning "请手动升级g++到10或更高版本"
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
else
|
||||||
|
print_success "g++版本满足要求(10+)"
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# 安装依赖环境
|
# 安装依赖环境
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
@@ -416,6 +521,9 @@ install_dependencies() {
|
|||||||
|
|
||||||
install_pm2
|
install_pm2
|
||||||
print_success "依赖环境安装完成"
|
print_success "依赖环境安装完成"
|
||||||
|
|
||||||
|
# 检查并升级C++编译器(如果需要)
|
||||||
|
check_cpp_compiler
|
||||||
echo ""
|
echo ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2024,6 +2132,9 @@ update_install_dependencies() {
|
|||||||
|
|
||||||
|
|
||||||
# 清理旧的node_modules
|
# 清理旧的node_modules
|
||||||
|
|
||||||
|
# 检查并升级C++编译器(如果需要)
|
||||||
|
check_cpp_compiler
|
||||||
if [[ -d "node_modules" ]]; then
|
if [[ -d "node_modules" ]]; then
|
||||||
print_info "清理旧依赖..."
|
print_info "清理旧依赖..."
|
||||||
rm -rf node_modules package-lock.json
|
rm -rf node_modules package-lock.json
|
||||||
|
|||||||
Reference in New Issue
Block a user