Msys GCC编译的EXE提示无法执行二进制文件:格式错误求助
MSYS环境下GCC生成的可执行文件无法运行问题
偶尔使用的Msys环境中,GCC生成的可执行文件无法正常运行,执行pacman -Suy升级系统后问题依旧。以下是会话记录:
$ gcc -v array-indexing.c -o array-indexing.exe gcc -v array-indexing.c -o array-indexing.exe Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-msys/10.2.0/lto-wrapper.exe Target: x86_64-pc-msys Configured with: /c/_/S/gcc/src/gcc-10.2.0/configure --build=x86_64-pc-msys --prefix=/usr --libexecdir=/usr/lib --enable-bootstrap --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --with-arch=x86-64 --with-tune=generic --disable-multilib --enable-__cxa_atexit --with-dwarf2 --enable-languages=c,c++,fortran,lto --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --disable-win32-registry --disable-symvers --with-gnu-ld --with-gnu-as --disable-isl-version-check --enable-checking=release --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts Thread model: posix Supported LTO compression algorithms: zlib gcc version 10.2.0 (GCC) COLLECT_GCC_OPTIONS='-v' '-o' 'array-indexing.exe' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-pc-msys/10.2.0/cc1.exe -quiet -v -idirafter /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/../include/w32api -idirafter /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/lib/../lib/../../include/w32api array-indexing.c -quiet -dumpbase array-indexing.c -mtune=generic -march=x86-64 -auxbase array-indexing -version -o /tmp/ccHxGOyu.s GNU C17 (GCC) version 10.2.0 (x86_64-pc-msys) compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version isl-0.22.1-GMP warning: GMP header version 6.2.0 differs from library version 6.2.1. warning: MPC header version 1.1.0 differs from library version 1.2.1. GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/usr/local/include" ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/include" ignoring duplicate directory "/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/lib/../lib/../../include/w32api" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-pc-msys/10.2.0/include /usr/lib/gcc/x86_64-pc-msys/10.2.0/include-fixed /usr/include /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/../include/w32api End of search list. GNU C17 (GCC) version 10.2.0 (x86_64-pc-msys) compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version isl-0.22.1-GMP warning: GMP header version 6.2.0 differs from library version 6.2.1. warning: MPC header version 1.1.0 differs from library version 1.2.1. GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 5dc831ba8ba50cc9f8b7e3207bc4f700 COLLECT_GCC_OPTIONS='-v' '-o' 'array-indexing.exe' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/bin/as.exe -v -o /tmp/ccyivrKf.o /tmp/ccHxGOyu.s GNU assembler version 2.36.1 (x86_64-pc-msys) using BFD version (GNU Binutils) 2.36.1 COMPILER_PATH=/usr/lib/gcc/x86_64-pc-msys/10.2.0/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/:/usr/lib/gcc/x86_64-pc-msys/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/:/usr/lib/gcc/x86_64-pc-msys/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/bin/ LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-msys/10.2.0/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/lib/../lib/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/lib/:/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-o' 'array-indexing.exe' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-pc-msys/10.2.0/collect2.exe -plugin /usr/lib/gcc/x86_64-pc-msys/10.2.0/msys-lto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-pc-msys/10.2.0/lto-wrapper.exe -plugin-opt=-fresolution=/tmp/cc7hM2x2.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmsys-2.0 -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZdlPv --wrap _ZdaPv --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPvRKSt9nothrow_t -Bdynamic --dll-search-prefix=msys- --tsaware -o array-indexing.exe /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/crt0.o /usr/lib/gcc/x86_64-pc-msys/10.2.0/crtbegin.o -L/usr/lib/gcc/x86_64-pc-msys/10.2.0 -L/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/lib/../lib -L/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/lib -L/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../.. /tmp/ccyivrKf.o -lgcc_s -lgcc -lmsys-2.0 -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/default-manifest.o /usr/lib/gcc/x86_64-pc-msys/10.2.0/crtend.o COLLECT_GCC_OPTIONS='-v' '-o' 'array-indexing.exe' '-mtune=generic' '-march=x86-64' $ file array-indexing.exe file array-indexing.exe array-indexing.exe: PE32+ executable (console) x86-64, for MS Windows $ ./array-indexing.exe ./array-indexing.exe bash: ./array-indexing.exe: cannot execute binary file: Exec format error $
从输出看,生成的文件被识别为Windows PE32+ x86-64控制台可执行文件,但在bash中运行时提示格式错误。编译过程中存在两条版本不匹配警告:
warning: GMP header version 6.2.0 differs from library version 6.2.1. warning: MPC header version 1.1.0 differs from library version 1.2.1.
解决方案
1. 修复GMP/MPC版本不匹配问题
环境依赖版本不一致可能导致编译产物异常,执行以下命令强制同步头文件与库版本:
pacman -S --force msys2-devel gmp mpc
清理编译缓存后重新编译:
rm -f array-indexing.exe /tmp/cc* gcc array-indexing.c -o array-indexing.exe
2. 排查可执行文件运行兼容性
- 切换到Windows原生命令行(cmd或PowerShell),直接执行
array-indexing.exe,验证文件本身是否可正常运行 - 检查MSYS2架构一致性:执行
uname -m,确认输出为x86_64,与编译目标架构匹配 - 禁用LTO编译尝试:LTO可能导致链接阶段异常,添加
-fno-lto参数重新编译:gcc -fno-lto array-indexing.c -o array-indexing.exe - 检查MSYS运行时库:确保
msys-2.0.dll在系统PATH中,或复制到可执行文件同目录
3. 重置MSYS2环境(以上方案无效时)
备份个人文件后,卸载当前MSYS2,安装最新版本,完成系统升级后重新安装GCC:
pacman -Syu pacman -S gcc
内容的提问来源于stack exchange,提问作者Peter - Reinstate Monica
相关产品推荐
相关产品推荐

