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

编译gcc-9.1.0时libgfortran相关strnlen声明错误求助

解决gcc-9.1.0编译时strnlen声明冲突问题

在Ubuntu 16.04.5 LTS上编译gcc-9.1.0(或单独编译gfortran)时,出现以下错误:

../../../gcc-9.1.0/libgfortran/runtime/string.c:96:1: error: static declaration of ‘strnlen’ follows non-static declaration

该错误的核心原因是:系统头文件(如/usr/include/string.h)中已经提供了非静态的strnlen函数声明,而gcc-9.1.0的libgfortran模块里又重复定义了一个静态版本的strnlen,二者冲突导致编译失败。以下是具体解决方法:

方法1:修改libgfortran的string.c文件

直接删除或注释掉libgfortran中静态声明的strnlen函数:

  1. 打开文件 ../gcc-9.1.0/libgfortran/runtime/string.c
  2. 定位到第96行附近的strnlen静态定义代码:
    static size_t
    strnlen (const char *s, size_t maxlen)
    {
      const char *p = s;
      while (maxlen-- > 0 && *p != '\0')
        p++;
      return p - s;
    }
    
  3. 直接删除这段代码,或者用/* */将其注释掉。

方法2:调整configure参数

在configure命令中添加--disable-libgfortran-backtrace参数,禁用libgfortran的回溯功能,减少潜在的函数冲突概率:

../gcc-9.1.0/configure --prefix=$EXTERNALPACKAGES/$PACKAGE/$VER-fortran \
   --program-suffix='-9' \
   --without-included-gettext \
   --enable-threads=posix \
   --enable-libstdcxx-time=yes \
   --with-system-zlib \
   --disable-browser-plugin \
   --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu \
   --enable-shared \
   --enable-languages=fortran \
   --with-mpfr="$MPFR_DIR" \
   --with-mpc=$MPC_DIR\
   --with-gmp=$GMP_DIR\
   --with-isl=$ISL_DIR\
   --enable-host-shared \
   --disable-multilib \
   --disable-libquadmath --disable-libquadmath-support --disable-werror\
   --disable-libgfortran-backtrace \
   CC=gcc-9 CXX=g++-9

额外验证步骤

可以先确认系统是否已经提供strnlen函数:

grep -n strnlen /usr/include/string.h

如果有输出结果,说明系统头文件中已包含该函数的声明,完全可以安全删除libgfortran中的重复定义。

说明:Ubuntu 16.04.5 LTS完全支持安装gfortran,此错误仅为编译阶段的函数声明冲突,按照上述方法处理后即可正常完成编译安装。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:24:27