2024年Windows下用MSYS2编译GDB时libiberty模块报错求助
问题场景
在Windows系统的MINGW64控制台编译GDB,已成功编译GMP和MPFR依赖,但执行编译流程时libiberty模块出现报错。使用的configure命令:
./configure --prefix=/devdash/install/ --with-gmp=/devdash/install --with-mpfr=/devdash/install --enable-static --disable-shared --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32
具体错误信息:
make[1]: Entering directory '/devdash/binutils-gdb' make[2]: Entering directory '/devdash/binutils-gdb/libiberty' if [ x"" != x ]; then \ gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I./../include -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -Wshadow=local -pedantic -D_GNU_SOURCE ./pex-win32.c -o pic/pex-win32.o; \ else true; fi if [ x"" != x ]; then \ gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I./../include -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -Wshadow=local -pedantic -D_GNU_SOURCE ./pex-win32.c -o noasan/pex-win32.o; \ else true; fi gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I./../include -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -Wshadow=local -pedantic -D_GNU_SOURCE ./pex-win32.c -o pex-win32.o ./pex-win32.c: In function ‘pex_win32_open_read’: ./pex-win32.c:122:10: warning: implicit declaration of function ‘_open’; did you mean ‘open’? [-Wimplicit-function-declaration] 122 | return _open (name, _O_RDONLY | (binary ? _O_BINARY : _O_TEXT)); | ^~~~~ | open ./pex-win32.c:122:23: error: ‘_O_RDONLY’ undeclared (first use in this function); did you mean ‘O_RDONLY’? 122 | return _open (name, _O_RDONLY | (binary ? _O_BINARY : _O_TEXT)); | ^~~~~~~~~ | O_RDONLY ./pex-win32.c:122:23: note: each undeclared identifier is reported only once for each function it appears in ./pex-win32.c:122:45: error: ‘_O_BINARY’ undeclared (first use in this function); did you mean ‘O_BINARY’? 122 | return _open (name, _O_RDONLY | (binary ? _O_BINARY : _O_TEXT)); | ^~~~~~~~~ | O_BINARY ./pex-win32.c:122:57: error: ‘_O_TEXT’ undeclared (first use in this function); did you mean ‘O_TEXT’? 122 | return _open (name, _O_RDONLY | (binary ? _O_BINARY : _O_TEXT)); | ^~~~~~~ | O_TEXT ./pex-win32.c: In function ‘pex_win32_open_write’: ./pex-win32.c:136:18: error: ‘_O_WRONLY’ undeclared (first use in this function); did you mean ‘O_WRONLY’? 136 | (_O_WRONLY | _O_CREAT | _O_TRUNC | ^~~~~~~~~ | O_WRONLY ./pex-win32.c:136:30: error: ‘_O_CREAT’ undeclared (first use in this function); did you mean ‘O_CREAT’? 136 | (_O_WRONLY | _O_CREAT | _O_TRUNC | ^~~~~~~~ | O_CREAT ./pex-win32.c:136:41: error: ‘_O_TRUNC’ undeclared (first use in this function); did you mean ‘O_TRUNC’? 136 | (_O_WRONLY | _O_CREAT | _O_TRUNC | ^~~~~~~~ | O_TRUNC ./pex-win32.c:137:30: error: ‘_O_BINARY’ undeclared (first use in this function); did you mean ‘O_BINARY’? 137 | | (binary ? _O_BINARY : _O_TEXT)), | ^~~~~~~~~ | O_BINARY ./pex-win32.c:137:42: error: ‘_O_TEXT’ undeclared (first use in this function); did you mean ‘O_TEXT’? 137 | | (binary ? _O_BINARY : _O_TEXT)), | ^~~~~~~ | O_TEXT ./pex-win32.c:138:17: error: ‘_S_IREAD’ undeclared (first use in this function); did you mean ‘S_IREAD’? 138 | _S_IREAD | _S_IWRITE); | ^~~~~~~~ | S_IREAD ./pex-win32.c:138:28: error: ‘_S_IWRITE’ undeclared (first use in this function); did you mean ‘S_IWRITE’? 138 | _S_IREAD | _S_IWRITE); | ^~~~~~~~~ | S_IWRITE ./pex-win32.c: In function ‘pex_win32_close’: ./pex-win32.c:146:10: warning: implicit declaration of function ‘_close’; did you mean ‘close’? [-Wimplicit-function-declaration] 146 | return _close (fd); | ^~~~~~ | close ./pex-win32.c: In function ‘win32_spawn’: ./pex-win32.c:670:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 670 | pid = (pid_t) pi->hProcess; | ^ ./pex-win32.c: In function ‘spawn_script’: ./pex-win32.c:695:31: error: ‘_O_RDONLY’ undeclared (first use in this function); did you mean ‘O_RDONLY’? 695 | int fd = _open (executable, _O_RDONLY); | ^~~~~~~~~ | O_RDONLY ./pex-win32.c:702:17: warning: implicit declaration of function ‘_read’; did you mean ‘read’? [-Wimplicit-function-declaration] 702 | int len = _read (fd, buf, sizeof (buf) - 1); | ^~~~~ | read ./pex-win32.c: In function ‘pex_win32_exec_child’: ./pex-win32.c:800:8: warning: implicit declaration of function ‘_dup’; did you mean ‘dup’? [-Wimplicit-function-declaration] 800 | in = _dup (orig_in); | ^~~~ | dup ./pex-win32.c: In function ‘pex_win32_wait’: ./pex-win32.c:932:7: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 932 | h = (HANDLE) pid; | ^ ./pex-win32.c: In function ‘pex_win32_pipe’: ./pex-win32.c:964:10: warning: implicit declaration of function ‘_pipe’; did you mean ‘pipe’? [-Wimplicit-function-declaration] 964 | return _pipe (p, 256, (binary ? _O_BINARY : _O_TEXT) | _O_NOINHERIT); | ^~~~~ | pipe ./pex-win32.c:964:35: error: ‘_O_BINARY’ undeclared (first use in this function); did you mean ‘O_BINARY’? 964 | return _pipe (p, 256, (binary ? _O_BINARY : _O_TEXT) | _O_NOINHERIT); | ^~~~~~~~~ | O_BINARY ./pex-win32.c:964:47: error: ‘_O_TEXT’ undeclared (first use in this function); did you mean ‘O_TEXT’? 964 | return _pipe (p, 256, (binary ? _O_BINARY : _O_TEXT) | _O_NOINHERIT); | ^~~~~~~ | O_TEXT ./pex-win32.c:964:58: error: ‘_O_NOINHERIT’ undeclared (first use in this function) 964 | return _pipe (p, 256, (binary ? _O_BINARY : _O_TEXT) | _O_NOINHERIT); | ^~~~~~~~~~~~ ./pex-win32.c:965:1: warning: control reaches end of non-void function [-Wreturn-type] 965 | } | ^ ./pex-win32.c: In function ‘pex_win32_open_write’: ./pex-win32.c:139:1: warning: control reaches end of non-void function [-Wreturn-type] 139 | } | ^ ./pex-win32.c: In function ‘pex_win32_open_read’: ./pex-win32.c:123:1: warning: control reaches end of non-void function [-Wreturn-type] 123 | } | ^ make[2]: *** [Makefile:1174: pex-win32.o] Error 1 make[2]: Leaving directory '/devdash/binutils-gdb/libiberty' make[1]: *** [Makefile:9555: all-libiberty] Error 2 make[1]: Leaving directory '/devdash/binutils-gdb' make: *** [Makefile:1041: all] Error 2
解决方法
这些错误源于MINGW64环境下,代码中带下划线前缀的Windows原生API宏定义(如_O_RDONLY)和函数(如_open)未被正确识别,本质是configure过程未启用必要的Windows编译选项,或头文件包含不全。
方法1:调整configure参数
在原命令中添加--enable-win32-registry和--with-system-zlib(系统需已安装zlib),确保编译器正确识别Windows头文件。修改后的configure命令:
./configure --prefix=/devdash/install/ --with-gmp=/devdash/install --with-mpfr=/devdash/install --enable-static --disable-shared --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-win32-registry --with-system-zlib
方法2:手动修改pex-win32.c(临时修复)
若调整参数后仍报错,直接修改libiberty/pex-win32.c:
- 在文件开头添加必要头文件:
#include <fcntl.h> #include <io.h> - 或替换所有带下划线的宏与函数为MINGW兼容版本:
_open→open_close→close_read→read_dup→dup_pipe→pipe_O_RDONLY→O_RDONLY_O_WRONLY→O_WRONLY_O_CREAT→O_CREAT_O_TRUNC→O_TRUNC_O_BINARY→O_BINARY_O_TEXT→O_TEXT_S_IREAD→S_IREAD_S_IWRITE→S_IWRITE_O_NOINHERIT→O_NOINHERIT
方法3:重新编译
执行以下命令清理旧编译文件并重新编译:
make clean ./configure [修改后的参数] make -j$(nproc) make install
补充说明
MINGW64环境编译GDB时,建议使用官方维护的MINGW-w64工具链,确保GMP、MPFR、zlib等依赖库均用同一工具链编译,避免链接错误。
内容的提问来源于stack exchange,提问作者developer
相关产品推荐
相关产品推荐

