简体   繁体   English

在C中,编译时找到全局const变量值

[英]In C, find global const variable value when compiling

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

I build a really simple code, and compile it with 我构建了一个非常简单的代码,并使用

gcc t.c -g -o t.out

Can I use objdump or nm or any tools to make sure the const variable value? 我可以使用objdump或nm或任何工具来确保const变量值吗? I always find address of variable "hello", but cannot find value 我总是找到变量“ hello”的地址,但找不到值

Anyone can help me, thanks a lot 任何人都可以帮助我,非常感谢

The example code 示例代码

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

compile with 用...编译

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

dump the content with 转储内容

objdump -xDSs fortests | less

(dumped a bit too much, but costs nothing, so ... meh ...) and search for hello (倾销太多,但不花任何钱,所以……嗯……)并寻找hello

0000000000400594 g     O .rodata        0000000000000004              hello

That means it is in the section .rodata . 这意味着它在.rodata部分中。 We explicitly asked objdump to list the contents of all sections, so here we have it, the value 0xdeadbeef . 我们明确要求objdump列出所有部分的内容,因此这里有了它,值为0xdeadbeef

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

And it should be clear by now, why you had trouble to find it. 现在应该很清楚,为什么您找不到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM