c - C中,编译时查找全局const变量值

标签 c variables toolchain

const int hello= 0x1111;
int main(){
}

我构建了一个非常简单的代码,并使用它进行编译

gcc t.c -g -o t.out

我可以使用 objdump 或 nm 或任何工具来确定 const 变量值吗? 我总是找到变量“hello”的地址,但找不到值

谁能帮帮我,非常感谢

最佳答案

示例代码

const int hello = 0xdeadbeef;
int main()
{
  return 0;
}

编译

gcc-4.9   -W -Wall -Wextra -pedantic  -std=c11 fortests.c -o fortests

转储内容

objdump -xDSs fortests | less

(转储有点太多,但没有任何成本,所以...嗯...)并搜索 hello

0000000000400594 g     O .rodata        0000000000000004              hello

这意味着它位于 .rodata 部分中。我们明确要求 objdump 列出所有部分的内容,因此我们在这里得到了值 0xdeadbeef。

Contents of section .rodata:
  400590 01000200 efbeadde                    ........ 
                  ^^^^^^^^
             here ||||||||

现在应该很清楚为什么你找不到它了。

关于c - C中,编译时查找全局const变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40477868/

相关文章:

python - 在常见的实现中,realloc 实际上会缩小缓冲区吗?

C 表达式必须是可修改的左值返回指针

android - 运行Flutter Doctor时Android许可证状态未知

c++ - 在哪里可以搜索免费的 C/C++ 库?

c - 隐藏结构成员

javascript - 变量递增返回 NaN

核心转储 GDB "Backtrace stopped: frame did not save the PC"

makefile - 如何在buildroot中设置 "BR2_PACKAGE_HOST_ENVIRONMENT_SETUP"

variables - 以路径作为变量的 Export-CSV

定义中函数标题后的 C 变量声明