微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何指示MSVC在可执行文件中嵌入符号?

如何解决如何指示MSVC在可执行文件中嵌入符号?

当我用MinGW编译程序时,符号会嵌入到可执行文件中:

$ cat hello.c
#include <stdio.h>

int main() {
    puts("Hello,world!");
}

$ x86_64-w64-mingw32-gcc hello.c -o hello.exe
$ nm hello.exe | grep main
0000000000402b50 T __getmainargs
000000000040829c I __imp___getmainargs
00000000004015e0 T __main
0000000000402c90 T main
00000000004014e0 T mainCRTStartup
0000000000407010 b mainret
0000000000407968 B __mingw_winmain_hInstance
0000000000407960 B __mingw_winmain_lpCmdLine
0000000000403000 D __mingw_winmain_nShowCmd
0000000000403024 D __native_dllmain_reason
0000000000401180 t __tmainCRTStartup

我想用MSVC编译器复制它:

$ cl hello.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29112 for x64
copyright (C) Microsoft Corporation.  All rights reserved.

hello.c
Microsoft (R) Incremental Linker Version 14.27.29112.0
copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj
$ nm hello.exe
nm: hello.exe: no symbols

使用/debug/Zi标志,结果相似-创建了一个pdb文件,但是可执行文件中没有符号。

解决方法

Visual C ++不支持在输出二进制文件中嵌入调试符号。 GCC + GDB支持此功能,而Visual C ++不支持。

https://docs.microsoft.com/en-us/cpp/build/reference/debug-generate-debug-info?redirectedfrom=MSDN&view=msvc-160

无法创建包含调试信息的.exe或.dll。调试信息总是放在.obj或.pdb文件中。


作为一种替代方法,您可以将输出PDB文件嵌入为Win32资源和extract that file on launch via a command-line switch for when you want to debug it。您需要一些自定义的 post -build步骤来完成此操作,因为rc步骤发生在生成最终EXE之前。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。