解决编译mips gcc 5.2.0版本时报错:mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'

解决mips交叉编译器mpfr编译错误
本文介绍在Ubuntu16.04环境下编译mips交叉编译器(gcc5.2.0)时遇到的mpfr编译错误及解决方法。错误出现在mpfr-longlong.h文件中,与GCC版本不兼容有关。通过修改mpfr-longlong.h文件中的条件编译指令解决了问题。

因为厂商提供mips交叉编译器不太满足要求,今天在ubuntu 16.04下尝试编译mips交叉编译器(gcc 5.2.0),在编译到mpfr时报了个错:

/bin/bash ./libtool --tag=CC   --mode=compile mips-linux-gnu-gcc -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DTIME_WITH_SYS_TIME=1 -DHAVE_LOCALE_H=1 -DHAVE_WCHAR_H=1 -DHAVE_STDARG=1 -DHAVE_SYS_TIME_H=1 -DHAVE_STDINT_H=1 -DHAVE_VA_COPY=1 -DHAVE_SETLOCALE=1 -DHAVE_GETTIMEOFDAY=1 -DHAVE_LONG_LONG=1 -DHAVE_INTMAX_T=1 -DMPFR_HAVE_FESETROUND=1 -DHAVE_ROUND=1 -DHAVE_TRUNC=1 -DHAVE_FLOOR=1 -DHAVE_CEIL=1 -DHAVE_NEARBYINT=1 -DLT_OBJDIR=\".libs/\" -DHAVE_ATTRIBUTE_MODE=1 -DHAVE_ALLOCA_H=1 -I. -I../.././mpfr   -I/home/gyd/Downloads/gcc-5.2.0/host-mips-linux-gnu/gmp  -g -O2 -MT mul.lo -MD -MP -MF .deps/mul.Tpo -c -o mul.lo ../.././mpfr/mul.c
libtool: compile:  mips-linux-gnu-gcc -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DTIME_WITH_SYS_TIME=1 -DHAVE_LOCALE_H=1 -DHAVE_WCHAR_H=1 -DHAVE_STDARG=1 -DHAVE_SYS_TIME_H=1 -DHAVE_STDINT_H=1 -DHAVE_VA_COPY=1 -DHAVE_SETLOCALE=1 -DHAVE_GETTIMEOFDAY=1 -DHAVE_LONG_LONG=1 -DHAVE_INTMAX_T=1 -DMPFR_HAVE_FESETROUND=1 -DHAVE_ROUND=1 -DHAVE_TRUNC=1 -DHAVE_FLOOR=1 -DHAVE_CEIL=1 -DHAVE_NEARBYINT=1 -DLT_OBJDIR=\".libs/\" -DHAVE_ATTRIBUTE_MODE=1 -DHAVE_ALLOCA_H=1 -I. -I../.././mpfr -I/home/gyd/Downloads/gcc-5.2.0/host-mips-linux-gnu/gmp -g -O2 -MT mul.lo -MD -MP -MF .deps/mul.Tpo -c ../.././mpfr/mul.c -o mul.o
In file included from ../.././mpfr/mpfr-impl.h:87:0,
                 from ../.././mpfr/mul.c:24:
../.././mpfr/mul.c: In function 'mpfr_mul':
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:315:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (tmp[1], tmp[0], MPFR_MANT (b)[0], MPFR_MANT (c)[0]);
           ^
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:322:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (tmp[1], tmp[0], MPFR_MANT (b)[0], MPFR_MANT (c)[0]);
           ^
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:323:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (tmp[2], t, MPFR_MANT (b)[1], MPFR_MANT (c)[0]);
           ^
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:332:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (tmp[1], tmp[0], MPFR_MANT (b)[0], MPFR_MANT (c)[0]);
           ^
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:333:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (tmp[2], t1, MPFR_MANT (b)[1], MPFR_MANT (c)[0]);
           ^
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:336:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (t1, t2, MPFR_MANT (b)[0], MPFR_MANT (c)[1]);
           ^
../.././mpfr/mpfr-longlong.h:1016:3: error: impossible constraint in 'asm'
   __asm__ ("multu %2,%3" : "=l" (w0), "=h" (w1) : "d" (u), "d" (v))
   ^
../.././mpfr/mul.c:337:11: note: in expansion of macro 'umul_ppmm'
           umul_ppmm (tmp[3], t3, MPFR_MANT (b)[1], MPFR_MANT (c)[1]);
           ^
Makefile:729: recipe for target 'mul.lo' failed
make[3]: *** [mul.lo] Error 1
make[3]: Leaving directory '/home/gyd/Downloads/gcc-5.2.0/host-mips-linux-gnu/mpfr'
Makefile:1452: recipe for target 'all-recursive' failed
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory '/home/gyd/Downloads/gcc-5.2.0/host-mips-linux-gnu/mpfr'
Makefile:5001: recipe for target 'all-mpfr' failed
make[1]: *** [all-mpfr] Error 2
make[1]: Leaving directory '/home/gyd/Downloads/gcc-5.2.0'
Makefile:879: recipe for target 'all' failed
make: *** [all] Error 2

貌似问题出在mpfr上,
google上找到了这篇bug report:《Bug 69134 - building a mips-cross compiler with in-tree mpfr-2.4.2 fails》
才搞明白原因是mpr-2.4.2 对mips的支持在高版本gcc下有问题。
文中也附了解决办法,如下图所示修改mpfr-longlong.h,将1014和1027行的#if __GNUC__ > 2 || __GNUC_MINOR__ >= 7改为#if __GNUC__ < 4,目的就是让编译器走#else分支(ubuntu16.04的gcc版本是5.4.0),再重新编译即可
这里写图片描述

附上编译mips-gcc-5.2.0的脚本

#! /bin/bash
# 基于君正发布的X1000开发板编译器(mips-gcc520-glibc222),编译gcc 5.2.0,增加-ffunction-sections -fdata-sections编译选项
sh_folder=$(dirname $(readlink -f $0))
with_sysroot=true
[ "$1" == "with_sysroot" ] && with_sysroot=true
pushd $sh_folder
# old mips gcc compiler location
mips_gcc=/opt/toolchains/mips-gcc520-glibc222
install_prefix=$sh_folder/release/opt/toolchains/mipsel-gcc520-gdface
old_sysroot=$mips_gcc/mips-linux-gnu/libc
target_sysroot=$old_sysroot
if [ $with_sysroot ];
then
    # 复制原编译器的 build tools 到安装路径
    target_tools=$install_prefix/mipsel-linux-gnu
    target_sysroot=$target_tools/libc
    [ -d "$target_sysroot" ] && rm "$target_sysroot" -fr
    mkdir -p "$target_tools" || exit -1
    echo "copy build tools to $install_prefix"
    cp -arf "$(dirname $old_sysroot)"/* "$target_tools" || exit -1
fi
# download gcc source package
# wget http://ftp.gnu.org/gnu/gcc/gcc-5.2.0/gcc-5.2.0.tar.bz2 || exit -1
# tar xvf gcc-5.2.0.tar.bz2 || exit -1
# gcc source folder
gcc_folder=gcc-5.2.0
pushd $gcc_folder || exit -1
# download dependencies 
# ./contrib/download_prerequisites 
# patch for mpfr-2.4.2
# ../patch_mpfr.sh
[   -d build ] && rm build -fr
[ ! -d build ] && mkdir build
pushd build
# X1000为小端(little endian),所以这里--target要改为mipsel-linux-gnu,否则编译不通过
../configure --with-pkgversion='Gdface 2018.06'\
    --build=x86_64-linux-gnu \
    --host=x86_64-linux-gnu \
    --target=mipsel-linux-gnu \
    --enable-threads \
    --disable-libmudflap \
    --disable-libssp \
    --disable-libstdcxx-pch \
    --with-arch-32=mips32r2 \
    --with-arch-64=mips64r2 \
    --with-float=hard \
    --with-mips-plt \
    --enable-extra-sgxxlite-multilibs \
    --with-specs='-D__CS_SOURCERYGXX_MAJ__=2015 -D__CS_SOURCERYGXX_MIN__=11 -D__CS_SOURCERYGXX_REV__=32' \
    --enable-languages=c,c++ \
    --enable-shared \
    --enable-static \
    --enable-lto \
    --enable-symvers=gnu \
    --enable-__cxa_atexit \
    --with-glibc-version=2.22 \
    --disable-nls \
    --prefix=$install_prefix \
    --with-sysroot=$target_sysroot \
    --with-build-sysroot=$target_sysroot \
    --enable-libgomp \
    --disable-libitm \
    --enable-libatomic \
    --disable-libssp \
    --disable-libcc1 \
    --with-endian=big \
    --enable-poison-system-directories \
    --with-python-dir=mipsel-linux-gnu/share/gdb/python \
    --with-build-time-tools=$mips_gcc/mips-linux-gnu/bin \
    CFLAGS="-ffunction-sections -fdata-sections" \
    CXXFLAGS="-ffunction-sections -fdata-sections" \
    SED=sed || exit -1
make -j8 || exit -1
make install || exit -1
popd
popd
popd

参考资料

《Bug 69134 - building a mips-cross compiler with in-tree mpfr-2.4.2 fails》

<think>嗯,用户现在遇到了编译工具链找不到的问题。之前用户提到过mips-mti-elf-gcc明明存在于/opt/mips-mti-elf/2016.05-08/bin目录下,但编译却说找不到,应该是环境变量配置的问题。用户可能是在嵌入式开发环境工作,正在交叉编译MIPS架构的程序。从路径命名看用的是MentorGraphics(现在变成SiemensEDA)的工具链。这类问题在嵌入式开发中很常见,特别是切换不同版本工具链。我上次已经给出了四个排查方向:环境变量检查、脚本检查、工具验证和兼容性排查。用户这次提问提到了"参考站内引用",可能是指上次的回答。需要确认用户是否已经尝试过之前的建议?但用户没有具体说明做了哪些排查,可能需要更详细的交互。用户的状态应该是比较着急的,因为工具链问题会阻塞整个开发流程。ta可能已经反复尝试过各种方法,但找不到症结所在。考虑到Linux环境变量的复杂性,需要把各种可能性都覆盖到。这次可以补充几个新角度:-检查工具链目录的权限问题,有用户可能没有执行权限-使用绝对路径直接调用工具链,看是否是shell的hash缓存导致-检查编译脚本是否有特殊的环境设置覆盖了PATH-尝试用strace跟踪编译过程查看实际查找路径上次给出的解决方案里,忘记提权限问题这个常见因素了。另外,提到检查脚本应该建议用户使用env-i纯净环境测试,这样能排除外部干扰。用户提供的路径很完整,说明ta对系统有一定了解,提问质量很高。但2016年的工具链确实比较旧了,兼容性问题概率较大。下次可以建议用户测试新版本工具链。考虑到交叉编译的复杂性,可能需要更具体的日志分析。用户如果提供完整的编译错误输出会更有帮助。不过现在给出的解决方案步骤已经很具体了,应该足够用户一步步排查。</think> 排查编译无法找到MIPS工具链的问题,可按以下步骤逐步检查环境变量和路径配置: --- ### **1. 检查环境变量PATH** 工具链必须位于系统`PATH`中,否则编译器无法自动定位: ```bash echo $PATH ``` ✅ **验证**: - 确保路径`/opt/mips-mti-elf/2016.05-08/bin`出现在输出结果中 - 若未出现,需手动添加: ```bash # 临生效(当前终端) export PATH="/opt/mips-mti-elf/2016.05-08/bin:$PATH" # 永久生效(添加到shell配置文件) echo &#39;export PATH="/opt/mips-mti-elf/2016.05-08/bin:$PATH"&#39; >> ~/.bashrc source ~/.bashrc # 立即生效 ``` --- ### **2. 检查工具链权限** 确保工具链可执行权限正常: ```bash ls -l /opt/mips-mti-elf/2016.05-08/bin/mips-mti-elf-gcc ``` ✅ **验证**: - 输出应包含`x`(执行权限),例如:`-rwxr-xr-x` - 若无权限,运行: ```bash chmod +x /opt/mips-mti-elf/2016.05-08/bin/* ``` --- ### **3. 直接测试工具链** 排除工具链自身问题: ```bash /opt/mips-mti-elf/2016.05-08/bin/mips-mti-elf-gcc --version ``` ✅ **预期输出**: ``` mips-mti-elf-gcc (MIPS MTI Embedded) 2016.05-08 ... ``` ❌ **若报错**: - `No such file or directory` → 检查工具链安装是否完整 - `Permission denied` → 参考步骤2 - `exec format error` → **64位系统需安装32位兼容库**: ```bash sudo apt install libc6-i386 # Debian/Ubuntu ``` --- ### **4. 检查构建系统配置** 若通过IDE(如Eclipse)或构建脚本调用: - **IDE配置**:检查项目中工具链路径设置(通常在`Project Properties > C/C++ Build > Environment`) - **Makefile配置**:确认`CC`变量是否正确指向路径: ```makefile CC = /opt/mips-mti-elf/2016.05-08/bin/mips-mti-elf-gcc ``` --- ### **5. 符号链接验证** 某些构建系统要求工具链前缀匹配: ```bash # 创建标准前缀符号链接 ln -s /opt/mips-mti-elf/2016.05-08/bin/mips-mti-elf-gcc /usr/bin/mips-mti-elf-gcc ``` --- ### **6. 动态库依赖检查** 工具链依赖的库可能缺失: ```bash ldd /opt/mips-mti-elf/2016.05-08/bin/mips-mti-elf-gcc ``` ✅ **验证**: - 所有依赖库应显示为`found`(无`not found`) --- ### **典型问题解决方案** | 问题现象 | 解决方案 | |-----------------------------------|------------------------------------------| | `bash: mips-mti-elf-gcc: command not found` | 将工具链路径加入`PATH`(步骤1) | | `Permission denied` | 添加执行权限(步骤2) | | `No such file or directory` | 重新安装工具链或修复路径 | | `exec format error` | 安装32位兼容库`libc6-i386`(步骤3) | --- 📌 **验证修复**: ```bash # 重新检查是否识别 which mips-mti-elf-gcc # 应返回完整路径 mips-mti-elf-gcc --version # 应输出版本信息 ``` > ⚠️ **注意**:旧版工具链(2016)可能与新版系统存在兼容性问题,可考虑升级至2023年后的版本[^1]。 --- ### **相关问题** 1. **如何在CMake项目中指定MIPS交叉编译工具链?** 需创建`toolchain.cmake`文件定义`CMAKE_C_COMPILER`变量[^2]。 2. **为何安装工具链后仍需手动配置环境变量?** 安装程序通常不会自动修改用户级PATH,需手动添加或通过包管理器配置。 3. **如何验证MIPS工具链与目标硬件的兼容性?** 通过`-march`和`-mabi`参数指定指令集架构(如`mips32r2`/`o32`),编译测试程序后通过QEMU或硬件烧录验证。 [^1]: MTI工具链发布说明显示2018年后版本支持Ubuntu 20.04+的兼容层 [^2]: CMake官方文档建议在独立文件中设置交叉编译变量(Cross Compiling with CMake, Chapter 3
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值