c++ - 为什么我的程序在启动前就崩溃了?

标签 c++ c windows gdb mingw

该程序由带有 -g 标志、-static-libgcc 和 -static-libstdc++ 的 g++ 编译。没有包含优化标志。但出于某种原因,我无法进入主要。为什么?

$ nm -C test.exe | grep main
006c05b0 T __getmainargs
006b0ad0 T __main
0088d0e8 B __mingw_winmain_hInstance
0088d0e4 B __mingw_winmain_lpCmdLine
0088d0ec B __mingw_winmain_nShowCmd
006ce518 D __native_dllmain_reason
00401180 t __tmainCRTStartup
0088edc8 I _imp____getmainargs
007491c0 r jisx0213_to_ucs_main
00405f0c T main
00401570 T mainCRTStartup
00884010 b mainret
004a3371 T sqlite3_backup_remaining
0078ada0 r uhc_1_2charset_main
0078c440 r uhc_1_2uni_main_page81
007899a0 r uhc_2_2charset_main
0078db00 r uhc_2_2uni_main_pagea1

$ gdb test.exe
GNU gdb (pcx32) 7.3.50.20111127-cvs
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-w64-mingw32".
For bug reporting instructions, please see:
...
Reading symbols from c:\test.exe
...done.
(gdb) break main
Breakpoint 1 at 0x405f15: file test.cpp, line 1054.
(gdb) break mainCRTStartup
Breakpoint 2 at 0x401570
(gdb) break __tmainCRTStartup
Breakpoint 3 at 0x40118c
(gdb) break __main
Breakpoint 4 at 0x6b0ad0
(gdb) break __getmainargs
Breakpoint 5 at 0x6c05b0
(gdb) run
Starting program: c:\test.exe
[New Thread 5832.0xc0c]
During startup program exited with code 0xc0000022.
(gdb)

附言dependency walker 显示它无法打开 SYSNTFY.DLL 并且找不到 IEFRAME.DLL。然而,这并不新鲜,也不应该成为问题所在。

(gdb) info files
Symbols from "c:\test.exe".
Local exec file:
        `c:\test.exe',
        file type pei-i386.
        Entry point: 0x401570
        0x00401000 - 0x006c14c4 is .text
        0x006c2000 - 0x006ce5d0 is .data
        0x006cf000 - 0x0080c3e0 is .rdata
        0x0080d000 - 0x00883c58 is .eh_frame
        0x00884000 - 0x0088d178 is .bss
        0x0088e000 - 0x00891d40 is .idata
        0x00892000 - 0x00892038 is .CRT
        0x00893000 - 0x00893020 is .tls
(gdb) break *0x401570
Note: breakpoint 2 also set at pc 0x401570.
Breakpoint 6 at 0x401570
(gdb) run
Starting program: c:\test.exe
[New Thread 5332.0x28b0]
During startup program exited with code 0xc0000022.

这说明入口点确实是 __tmainCRTStartup,但 gdb 似乎没有到达那里。

答案就像评论一样:图书馆把一切都搞砸了。为了解决这个问题,我一个一个地取消了每个库的链接,直到它设法进入主目录。

最佳答案

我怀疑您的静态或全局变量初始化会引发 SIGSEGV 或其他错误...所有静态和全局变量都在执行 main 之前初始化。

另外...我看到您正在运行 MinGW——您是否正确设置了指向 MinGW bin 目录的路径?当我构建 MinGW 应用程序(通过 Eclipse)时,我有一个启动器应用程序和两个都构建的应用程序。除非我的 Windows 路径上有 MinGW bin 目录,否则我必须使用启动器。

进一步阅读,值得注意的是 DLL 是在调用 mainCRTStartup 之前加载的。对于 Windoze 应用程序,您通常会中断 _DllMain 来处理它。我不确定在 MinGW 中如何/在何处处理?

关于c++ - 为什么我的程序在启动前就崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15911018/

相关文章:

c++ - 如何在 4 个字符串之间搜索字符串

C++ 运算符重载和复制构造函数

c - 在 C 中制作唯一值列表的理想方法是什么?

c++ - 在 C/C++ 中快速实现/逼近 pow() 函数

c++ - 将 gtest 配置为仅在控制台中显示失败的测试

windows - 如何枚举进程的句柄?

c++ - 堆栈调用 : why is the cursor jumping to a specific position of the recursive function

c - 对话框不工作 - win32 api

ruby-on-rails - 安装 Rails gem 时,rails.bat 如何安装到 bin 目录?

c++ - VS2010 带有 extern 关键字