You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Clang不为外部符号生成调试信息,是否有可用命令行选项?

解决Clang编译外部汇编符号无调试信息的问题

目前Clang没有与GCC -gdescribe-dies完全等价的命令行选项,但可以通过以下两种方式解决外部汇编符号无调试信息的问题:

方法一:在C代码中添加__attribute__((used))属性

Clang默认会忽略未在当前编译单元定义的外部符号的类型调试信息,通过添加__attribute__((used))可以强制Clang保留该符号的类型信息到调试符号中。

修改后的C代码:

extern int array[3] __attribute__((used));

int main()
{
  array[0] = 0;
  array[1] = 1;
  array[2] = 2;
}

编译命令保持不变:

clang test.asm~ test.c -o test -g

此时用GDB调试即可正常识别符号类型:

Reading symbols from test...
(gdb) p array[1]
$1 = 1
(gdb) ptype array
type = int [3]

方法二:为汇编文件添加DWARF调试信息

使用NASM的调试选项生成带DWARF信息的目标文件,Clang链接时会保留这些调试信息。

重新汇编源文件:

nasm test.asm -o test.o -f elf64 -g -F dwarf

再用Clang编译链接:

clang test.o test.c -o test -g

这种方式下,汇编符号本身携带调试类型信息,GDB同样可以直接识别并打印类型和值。


内容的提问来源于stack exchange,提问作者Silver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:12:37