c++ msys2中的Mingw64创建损坏的可执行文件

ryevplcw  于 2023-08-09  发布在  其他
关注(0)|答案(1)|浏览(95)

我目前正在使用 MSYS_NT-10.0-22621 Legion 3.4.7.x86_64 2023-07-14 16:57 UTC x86_64 Msys 和使用mingw 64
我的mingw 64版本

g++.exe (Rev7, Built by MSYS2 project) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

字符串
我写了一个简单的main.cpp文件,其中包含。

#include <iostream>

int main()
{
    std::cout<<"Hello world"<<std::endl;
}


然而,在构建它时,即使它创建了一个 .exe 文件,它也不会输出任何东西。
所以,我安装了一个以前的版本,使用Mingw本地。我的版本是

g++.exe (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


为了进行比较,我将同一个文件编译到程序集中。
12.3.0

.file   "main.cpp"
    .text
.lcomm _ZStL8__ioinit,1,1
    .def    __main; .scl    2;  .type   32; .endef
    .section .rdata,"dr"
.LC0:
    .ascii "Hello world\0"
    .text
    .globl  main
    .def    main;   .scl    2;  .type   32; .endef
    .seh_proc   main
main:
.LFB1991:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    call    __main
    leaq    .LC0(%rip), %rax
    movq    %rax, %rdx
    movq    .refptr._ZSt4cout(%rip), %rax
    movq    %rax, %rcx
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movq    %rax, %rcx
    movq    .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rax
    movq    %rax, %rdx
    call    _ZNSolsEPFRSoS_E
    movl    $0, %eax
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .def    __tcf_0;    .scl    3;  .type   32; .endef
    .seh_proc   __tcf_0
__tcf_0:
.LFB2524:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    leaq    _ZStL8__ioinit(%rip), %rax
    movq    %rax, %rcx
    call    _ZNSt8ios_base4InitD1Ev
    nop
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .def    _Z41__static_initialization_and_destruction_0ii;    .scl    3;  .type   32; .endef
    .seh_proc   _Z41__static_initialization_and_destruction_0ii
_Z41__static_initialization_and_destruction_0ii:
.LFB2523:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    movl    %ecx, 16(%rbp)
    movl    %edx, 24(%rbp)
    cmpl    $1, 16(%rbp)
    jne .L6
    cmpl    $65535, 24(%rbp)
    jne .L6
    leaq    _ZStL8__ioinit(%rip), %rax
    movq    %rax, %rcx
    call    _ZNSt8ios_base4InitC1Ev
    leaq    __tcf_0(%rip), %rax
    movq    %rax, %rcx
    call    atexit
.L6:
    nop
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .def    _GLOBAL__sub_I_main;    .scl    3;  .type   32; .endef
    .seh_proc   _GLOBAL__sub_I_main
_GLOBAL__sub_I_main:
.LFB2525:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    movl    $65535, %edx
    movl    $1, %ecx
    call    _Z41__static_initialization_and_destruction_0ii
    nop
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .section    .ctors,"w"
    .align 8
    .quad   _GLOBAL__sub_I_main
    .ident  "GCC: (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 12.3.0"
    .def    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;    .scl    2;  .type   32; .endef
    .def    _ZNSolsEPFRSoS_E;   .scl    2;  .type   32; .endef
    .def    _ZNSt8ios_base4InitD1Ev;    .scl    2;  .type   32; .endef
    .def    _ZNSt8ios_base4InitC1Ev;    .scl    2;  .type   32; .endef
    .def    atexit; .scl    2;  .type   32; .endef
    .section    .rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, "dr"
    .globl  .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .linkonce   discard
.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_:
    .quad   _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .section    .rdata$.refptr._ZSt4cout, "dr"
    .globl  .refptr._ZSt4cout
    .linkonce   discard
.refptr._ZSt4cout:
    .quad   _ZSt4cout


和13.1输出

.file   "main.cpp"
    .text
    .def    __main; .scl    2;  .type   32; .endef
    .section .rdata,"dr"
.LC0:
    .ascii "Hello world\0"
    .text
    .globl  main
    .def    main;   .scl    2;  .type   32; .endef
    .seh_proc   main
main:
.LFB2073:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    call    __main
    leaq    .LC0(%rip), %rax
    movq    %rax, %rdx
    movq    .refptr._ZSt4cout(%rip), %rax
    movq    %rax, %rcx
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movq    %rax, %rcx
    movq    .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rax
    movq    %rax, %rdx
    call    _ZNSolsEPFRSoS_E
    movl    $0, %eax
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .section .rdata,"dr"
_ZNSt8__detail30__integer_to_chars_is_unsignedIjEE:
    .byte   1
_ZNSt8__detail30__integer_to_chars_is_unsignedImEE:
    .byte   1
_ZNSt8__detail30__integer_to_chars_is_unsignedIyEE:
    .byte   1
    .ident  "GCC: (Rev7, Built by MSYS2 project) 13.1.0"
    .def    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;    .scl    2;  .type   32; .endef
    .def    _ZNSolsEPFRSoS_E;   .scl    2;  .type   32; .endef
    .section    .rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, "dr"
    .globl  .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .linkonce   discard
.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_:
    .quad   _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .section    .rdata$.refptr._ZSt4cout, "dr"
    .globl  .refptr._ZSt4cout
    .linkonce   discard
.refptr._ZSt4cout:
    .quad   _ZSt4cout


我的命令是相同的两个版本,(没有任何优化选项)

g++ .\main.cpp -S


对于可执行文件

g++ .\main.cpp -o .\main.exe


在msys 2shell、mingw 64 shell和ucrt 64 shell中使用pacman -Qq | grep gcc,我得到了这些包

gcc-libs
mingw-w64-x86_64-gcc
mingw-w64-x86_64-gcc-ada
mingw-w64-x86_64-gcc-fortran
mingw-w64-x86_64-gcc-libgfortran
mingw-w64-x86_64-gcc-libs
mingw-w64-x86_64-gcc-objc
mingw-w64-x86_64-libgccjit

  • 所有的贝壳都一样 *

当我在shell中运行$ pacman -Qi gcc-libs时,它返回:

Name            : gcc-libs
Version         : 11.3.0-4
Description     : Runtime libraries shipped by GCC
Architecture    : x86_64
URL             : https://gcc.gnu.org/
Licenses        : GPL  LGPL  FDL  custom
Groups          : None
Provides        : None
Depends On      : None
Optional Deps   : None
Required By     : brotli  bsdtar  file  inetutils  libasprintf  libassuan  libbz2  libdb  libexpat  libgdbm
                  libgettextpo  libgnutls  libiconv  libintl  libksba  liblz4  libnghttp2  libnpth  libpcre  libpcre2_8
                  libutil-linux  libzstd  ncurses  wget  zlib  zstd
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 5.30 MiB
Packager        : CI (msys2/msys2-autobuild/79096b75/5113523851)
Build Date      : Mon, May 29, 2023 10:03:27 PM
Install Date    : Tue, Jul 18, 2023 9:15:05 AM
Install Reason  : Installed as a dependency for another package
Install Script  : No
Validated By    : Signature


此外,为了获得更多信息,我添加了以下内容:

$ pacman -Qi | grep gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libbz2  libiconv  libexpat  liblzma  liblz4  libopenssl  libzstd  zlib
Depends On      : gcc-libs  zlib  libbz2  liblzma  libzstd
Name            : gcc-libs
Depends On      : gcc-libs  libintl  libreadline  ncurses
Depends On      : gcc-libs
Depends On      : gcc-libs  libgpg-error
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libreadline  libiconv  libintl  ncurses
Depends On      : gcc-libs
Depends On      : gcc-libs  libidn2  libiconv  libintl  gmp  libnettle  libp11-kit  libtasn1  zlib
Depends On      : gcc-libs  libintl
Depends On      : gcc-libs  libiconv
Depends On      : gcc-libs  libgpg-error
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libintl
Depends On      : gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-binutils  mingw-w64-x86_64-crt  mingw-w64-x86_64-headers  mingw-w64-x86_64-isl  mingw-w64-x86_64-libiconv  mingw-w64-x86_64-gmp  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-mpc  mingw-w64-x86_64-gcc-libs=13.1.0-7  mingw-w64-x86_64-windows-default-manifest  mingw-w64-x86_64-winpthreads  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-gcc-libs=13.1.0-7
Name            : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-mpc  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-python  mingw-w64-x86_64-readline  mingw-w64-x86_64-xxhash  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-expat  mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-libiconv
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gettext
Required By     : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-winpthreads-git
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gmp
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-expat  mingw-w64-x86_64-bzip2  mingw-w64-x86_64-libffi  mingw-w64-x86_64-mpdecimal  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-openssl  mingw-w64-x86_64-sqlite3  mingw-w64-x86_64-tcl  mingw-w64-x86_64-tk  mingw-w64-x86_64-zlib  mingw-w64-x86_64-xz  mingw-w64-x86_64-tzdata
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-termcap
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-readline  mingw-w64-x86_64-tcl  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libiconv  libidn2  libintl  libgpgme  libpcre2_8  libpsl  libuuid  libgnutls  zlib
Depends On      : gcc-libs
Depends On      : gcc-libs  libzstd

nurpr@Legion MSYS ~
$ pacman -Qi | grep mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-binutils  mingw-w64-x86_64-crt  mingw-w64-x86_64-headers  mingw-w64-x86_64-isl  mingw-w64-x86_64-libiconv  mingw-w64-x86_64-gmp  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-mpc  mingw-w64-x86_64-gcc-libs=13.1.0-7  mingw-w64-x86_64-windows-default-manifest  mingw-w64-x86_64-winpthreads  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-gcc-libs=13.1.0-7
Name            : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-mpc  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-python  mingw-w64-x86_64-readline  mingw-w64-x86_64-xxhash  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-expat  mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-libiconv
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gettext
Required By     : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-winpthreads-git
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gmp
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-expat  mingw-w64-x86_64-bzip2  mingw-w64-x86_64-libffi  mingw-w64-x86_64-mpdecimal  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-openssl  mingw-w64-x86_64-sqlite3  mingw-w64-x86_64-tcl  mingw-w64-x86_64-tk  mingw-w64-x86_64-zlib  mingw-w64-x86_64-xz  mingw-w64-x86_64-tzdata
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-termcap
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-readline  mingw-w64-x86_64-tcl  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs


运行从13.1生成的可执行文件不输出任何内容,12.3可执行文件正确输出。在我更新它之前,它一直在工作。我也使用了相同版本的Mingw,它也有同样的问题。

这是 g++ 13.1 中的某种bug吗?为什么不管用?

xdnvmnnf

xdnvmnnf1#

我安装了Qt,显然这把我的mingw64搞砸了。环境变量中的windows system path在msys2本身之前也有qt目录。
卸载qt并将其从system path中删除后,修复了此问题。

相关问题