diff --git a/install.sh b/install.sh index 66aabc7..931c6a1 100644 --- a/install.sh +++ b/install.sh @@ -380,6 +380,111 @@ EOF 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 print_success "依赖环境安装完成" + + # 检查并升级C++编译器(如果需要) + check_cpp_compiler echo "" } @@ -2024,6 +2132,9 @@ update_install_dependencies() { # 清理旧的node_modules + + # 检查并升级C++编译器(如果需要) + check_cpp_compiler if [[ -d "node_modules" ]]; then print_info "清理旧依赖..." rm -rf node_modules package-lock.json