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

编译安装GCC 14时执行make install报错求助

编译GCC 14时-Werror=format-security错误的解决

环境与问题背景

  • 工作目录:/home/m/Desktop/dev/gcc/gccbuild(构建目录)
  • 上级目录包含三个文件夹:gccgit(GCC源码克隆目录)、gccbuild(当前构建目录)、gccout(安装目标目录)
  • 执行安装命令时触发错误:
    make DESTDIR=$(realpath ../gccout) install
    

错误信息

g++ -std=c++11  -fno-PIE -c  -DIN_GCC_FRONTEND -g -O2     -DIN_GCC    -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Wconditionally-supported -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H -fno-PIE -I. -Ic -I../../gccgit/gcc -I../../gccgit/gcc/c -I../../gccgit/gcc/../include  -I../../gccgit/gcc/../libcpp/include -I../../gccgit/gcc/../libcody  -I../../gccgit/gcc/../libdecnumber -I../../gccgit/gcc/../libdecnumber/bid -I../libdecnumber -I../../gccgit/gcc/../libbacktrace   -o c/c-typeck.o -MT c/c-typeck.o -MMD -MP -MF c/.deps/c-typeck.TPo ../../gccgit/gcc/c/c-typeck.cc
../../gccgit/gcc/c/c-typeck.cc: In function ‘int convert_arguments(location_t, vec<unsigned int>, tree, vec<tree_node*, va_gc>*, vec<tree_node*, va_gc>*, tree, tree)’:
[...]
../../gccgit/gcc/c/c-typeck.cc:12212:16: error: format not a string literal and no format arguments [-Werror=format-security]
12212 |       error_at (location, invalid_op_diag);
      |       ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors

问题分析

  • -Wformat-security和-Werror是GCC自身构建系统在configure阶段生成的编译标志,默认开启是为了保障源码质量。
  • 错误本质是error_at函数的第二个参数invalid_op_diag为字符串变量而非字面量,触发-Wformat-security警告,而-Werror将所有警告转为错误导致编译终止。

解决方法

方法1:临时禁用特定警告的错误处理

直接在make命令中添加编译标志,覆盖默认设置:

make DESTDIR=$(realpath ../gccout) CXXFLAGS="-Wno-error=format-security" install

注:错误出在C++源码文件,只需指定CXXFLAGS;若涉及C代码,可同时添加CFLAGS="-Wno-error=format-security"。

方法2:修改源码适配格式要求

编辑gccgit/gcc/c/c-typeck.cc第12212行,将调用改为格式化字符串形式:

error_at (location, "%s", invalid_op_diag);

修改后重新执行安装命令即可。

方法3:重新配置构建目录关闭所有警告转错误

若未完成编译,可重新运行configure时添加--disable-werror选项:

cd ../gccbuild
../gccgit/configure --prefix=$(realpath ../gccout) --disable-werror [你的其他配置选项]
make
make install

注:此方法会关闭所有-Werror,可能忽略其他潜在问题,仅适合临时调试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 15:01:30