zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Windows10下使用Msys2编译gdb源码

2023-04-18 14:15:12 时间

Windows10下使用Msys2编译gdb10.2源码

下载msys2

直接搜索msys2进入官网下载,版本为msys2-x86_64-20210725.exe
网址:https://www.msys2.org

下载gdb源码

直接搜索gdb进入官网下载,版本为gdb-10.2.tar.gz
网址:http://www.gnu.org/software/gdb/

安装msys2

1.直接安装
2.安装完成后,打开msys64mingw32.exe

在这里插入图片描述

编译gdb-编译前准备

1.首先输入命令

$ cd gdb-10.2/

2.输入命令对msys进行更新并等待更新完成

$ pacman -Syuu

3.输入命令来下载一系列编译链

$ pacman -S mingw-w64-i686-toolchain

注意: "$ pacman -S mingw-w64-i686-toolchain"这个命令会找到指定的最新的工具包进行下载,但是由于编译的时候,有可能这些工具链发生了版本的更替,可能导致结果发生很大的变化。

输入命令后会显示出如下信息

:: There are 19 members in group mingw-w64-i686-toolchain:
:: Repository mingw32

1)mingw-w64-i686-binutils 2) mingw-w64-i686-crt-git 3) mingw-w64-i686-gcc
4) mingw-w64-i686-gcc-ada 5) mingw-w64-i686-gcc-fortran
6) mingw-w64-i686-gcc-libgfortran 7) mingw-w64-i686-gcc-libs
8) mingw-w64-i686-gcc-objc 9) mingw-w64-i686-gdb
10) mingw-w64-i686-gdb-multiarch 11) mingw-w64-i686-headers-git
12) mingw-w64-i686-libgccjit 13) mingw-w64-i686-libmangle-git
14) mingw-w64-i686-libwinpthread-git 15) mingw-w64-i686-make
16) mingw-w64-i686-pkgconf 17) mingw-w64-i686-tools-git
18) mingw-w64-i686-winpthreads-git 19) mingw-w64-i686-winstorecompat-git

Enter a selection (default=all):
resolving dependencies…
looking for conflicting packages…
Packages (47) mingw-w64-i686-bzip2-1.0.8-2
mingw-w64-i686-ca-certificates-20200601-3
mingw-w64-i686-expat-2.4.1-1 mingw-w64-i686-gettext-0.19.8.1-10
mingw-w64-i686-gmp-6.2.1-2 mingw-w64-i686-isl-0.24-1
mingw-w64-i686-libffi-3.3-4 mingw-w64-i686-libiconv-1.16-2
mingw-w64-i686-libsystre-1.0.1-4
mingw-w64-i686-libtasn1-4.17.0-1
mingw-w64-i686-libtre-git-r128.6fb7206-2
mingw-w64-i686-mpc-1.2.1-1 mingw-w64-i686-mpdecimal-2.5.0-2
mingw-w64-i686-mpfr-4.1.0-3 mingw-w64-i686-ncurses-6.2-3
mingw-w64-i686-openssl-1.1.1.k-3
mingw-w64-i686-p11-kit-0.23.22-2 mingw-w64-i686-python-3.9.6-2
mingw-w64-i686-readline-8.0.004-2
mingw-w64-i686-sqlite3-3.36.0-1 mingw-w64-i686-tcl-8.6.11-3
mingw-w64-i686-termcap-1.3.1-6 mingw-w64-i686-tk-8.6.11.1-2
mingw-w64-i686-windows-default-manifest-6.4-3
mingw-w64-i686-xxhash-0.8.0-1 mingw-w64-i686-xz-5.2.5-2
mingw-w64-i686-zlib-1.2.11-9 mingw-w64-i686-zstd-1.5.0-1
mingw-w64-i686-binutils-2.37-2
mingw-w64-i686-crt-git-9.0.0.6270.0e0eed623-1
mingw-w64-i686-gcc-10.3.0-5 mingw-w64-i686-gcc-ada-10.3.0-5
mingw-w64-i686-gcc-fortran-10.3.0-5
mingw-w64-i686-gcc-libgfortran-10.3.0-5
mingw-w64-i686-gcc-libs-10.3.0-5
mingw-w64-i686-gcc-objc-10.3.0-5 mingw-w64-i686-gdb-10.2-2
mingw-w64-i686-gdb-multiarch-10.2-2
mingw-w64-i686-headers-git-9.0.0.6270.0e0eed623-1
mingw-w64-i686-libgccjit-10.3.0-5
mingw-w64-i686-libmangle-git-9.0.0.6270.0e0eed623-1
mingw-w64-i686-libwinpthread-git-9.0.0.6270.0e0eed623-1
mingw-w64-i686-make-4.3-1 mingw-w64-i686-pkgconf-1.7.4-2
mingw-w64-i686-tools-git-9.0.0.6270.0e0eed623-1
mingw-w64-i686-winpthreads-git-9.0.0.6270.0e0eed623-1
mingw-w64-i686-winstorecompat-git-9.0.0.6270.0e0eed623-1

Total Download Size: 160.05 MiB
Total Installed Size: 1049.53 MiB

注意:详细安装链与工具包版本如上所示,一般输入 "pacman -S mingw-w64-i686-toolchain"命令回车即可不用选择,直接回车全部安装,等待安装完成即可。

4.安装中途出现的问题

error: failed retrieving file ‘mingw-w64-i686-zlib-1.2.11-9-any.pkg.tar.zst’ from mirror.msys2.org : Failed to connect to mirrors.tuna.tsinghua.edu.cn port 443: Connection timed out
error: failed retrieving file ‘mingw-w64-i686-crt-git-9.0.0.6270.0e0eed623-1-any.pkg.tar.zst’ from mirror.msys2.org : Failed to connect to mirrors.tuna.tsinghua.edu.cn port 443: Connection timed out

并没有发现对后续的影响,暂时无改动。

5.查看gcc版本(为后续大家在编译gdb源码提供一个信息,版本不同会存在很大问题)

$ gcc -v

输入命令会显示出如下信息

Using built-in specs.
COLLECT_GCC=E:msys64mingw32ingcc.exe
COLLECT_LTO_WRAPPER=E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: …/gcc-10.3.0/configure --prefix=/mingw32 --with-local-prefix=/mingw32/local --build=i686-w64-mingw32 --host=i686-w64-mingw32 --target=i686-w64-mingw32 --with-native-system-header-dir=/mingw32/i686-w64-mingw32/include --libexecdir=/mingw32/lib --enable-bootstrap --enable-checking=release --with-arch=i686 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw32 --with-mpfr=/mingw32 --with-mpc=/mingw32 --with-isl=/mingw32 --with-pkgversion=‘Rev5, Built by MSYS2 project’ --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --with-boot-ldflags=‘-pipe -Wl,–dynamicbase,–nxcompat,–no-seh -Wl,–large-address-aware -Wl,–disable-dynamicbase -static-libstdc++ -static-libgcc’ ‘LDFLAGS_FOR_TARGET=-pipe -Wl,–dynamicbase,–nxcompat,–no-seh -Wl,–large-address-aware’ --enable-linker-plugin-flags=‘LDFLAGS=-static-libstdc++ -static-libgcc -pipe -Wl,–dynamicbase,–nxcompat,–no-seh -Wl,–large-address-aware -Wl,–stack,12582912’ --disable-sjlj-exceptions --with-dwarf2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.3.0 (Rev5, Built by MSYS2 project)

6.安装make

$ pacman -S make

查看make版本:

$ make -v

输入命令后会显示出如下信息

GNU Make 4.3
Built for x86_64-pc-msys
Copyright © 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

7.安装makeinfo

$ pacman -S texinfo

输入命令后会显示出如下信息(注意版本信息)

resolving dependencies…
looking for conflicting packages…
Packages (1) texinfo-6.8-1
Total Download Size: 1.16 MiB
Total Installed Size: 8.21 MiB
:: Proceed with installation? [Y/n] Y

编译gdb-正式编译

1.输入命令

$ ./configure

等待完成

2.输入命令

$ make

等待完成

3.出现问题

E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/i686-w64-mingw32/bin/ld.exe: E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/lib/libncursesw.a(lib_termcap.o)😦.bss+0x4): multiple definition of UP; …/readline/readline/libreadline.a(terminal.o):E:msys64homechenyanggdb-10.2 eadline eadline/terminal.c:779: first defined here
E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/i686-w64-mingw32/bin/ld.exe: E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/lib/libncursesw.a(lib_termcap.o)😦.bss+0x0): multiple definition of BC;
…/readline/readline/libreadline.a(terminal.o):terminal.c:(.bss+0x58): first defined here
E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/i686-w64-mingw32/bin/ld.exe: E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/lib/libncursesw.a(lib_tputs.o)😦.bss+0x6): multiple definition of PC;
…/readline/readline/libreadline.a(terminal.o):terminal.c:(.bss+0x5c): first defined here
E:/msys64/mingw32/bin/…/lib/gcc/i686-w64-mingw32/10.3.0/…/…/…/…/i686-w64-mingw32/bin/ld.exe: …/gnulib/import/libgnu.a(getrandom.o): in function getrandom’:
E:msys64homechenyanggdb-10.2gnulibimport/getrandom.c:129: undefined reference to BCryptGenRandom@16
collect2.exe: error: ld returned 1 exit status
make[2]: *** [Makefile:1866: gdb.exe] Error 1
make[2]: Leaving directory ‘/home/chenyang/gdb-10.2/gdb’
make[1]: *** [Makefile:10412: all-gdb] Error 2
make[1]: Leaving directory ‘/home/chenyang/gdb-10.2’
make: *** [Makefile:866: all] Error 2

解决问题

打开gdb-10.2 eadline eadline erminal.c 定位于109行

#  endif /* __EMX__ || NEED_EXTERN_PC */
char PC, *BC, *UP;根据报错应注释此行
#endif /* !__linux__ && !NCURSES_VERSION */

修改为:

#  endif /* __EMX__ || NEED_EXTERN_PC */
//char PC, *BC, *UP;根据报错应注释此行
#endif /* !__linux__ && !NCURSES_VERSION */

打开gdb-10.2gnulibimportgetrandom.c 定位于128行

# if !HAVE_LIB_BCRYPT
      if (!initialized)
        initialize ();
# endif
      if (BCryptGenRandomFunc != NULL
          && BCryptGenRandomFunc (NULL, buffer, length,
                                  BCRYPT_USE_SYSTEM_PREFERRED_RNG)
             == 0 /*STATUS_SUCCESS*/)
        return length;
      bcrypt_not_working = 1;

修改为:

# if !HAVE_LIB_BCRYPT
      if (!initialized)
        initialize ();
      if (BCryptGenRandomFunc != NULL
          && BCryptGenRandomFunc (NULL, buffer, length,
                                  BCRYPT_USE_SYSTEM_PREFERRED_RNG)
             == 0 /*STATUS_SUCCESS*/)
        return length;
      bcrypt_not_working = 1;
# endif

重新编译

输入命令

$ make

显示如下信息后编译结束

make[4]: Leaving directory ‘/home/chenyang/gdb-10.2/gdb/data-directory’
make[3]: Leaving directory ‘/home/chenyang/gdb-10.2/gdb’
make[2]: Leaving directory ‘/home/chenyang/gdb-10.2/gdb’
make[1]: Nothing to be done for ‘all-target’.
make[1]: Leaving directory ‘/home/chenyang/gdb-10.2’

打开文件gdb-10.2gdb
在这里插入图片描述
根据修改时间就可以看到最新编译的gdb.exe了
注意:该exe并未使用,是否正常有待考证,编译方法仅供参考交流。