如何指示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之前。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...