0

我正在学习链接..
我用c编写了以下代码并使用gcc制作了.o

int f()
{
static int x=0;
return x;
}

extern int z;

int g()
{
static int x=10;
return x;
}

static int y;
static int y=9;

int main()
{
return 0;
}

然后我把它变成了.o:
gcc begin.c -o begin.o

现在,当我使用 readelf 检查 symtab 时,没有 z 的记录……为什么?
gcc如何允许两个'y'?
在 .data 部分中,两个“x”是如何区分的?

4

1 回答 1

0

I'm an a rank non-expert, but I can hazard a couple guesses:

  1. Since you only declare z but never actually use it, there is no need to maintain a reference to it.

  2. Try adding -Wall, which will tell you (at least) that y is declared but unused. The first y is actually just a declaration, which you can repeat indefinitely as long as you do not declare a conflicting type.

  3. In my compile, they end up being named x.1281 and x.1287, so I guess it's something like the name-mangling that happens with C++. The 1281 and 1287 I would guess are the offsets of some relevance, although I am not able to see anything obvious.

于 2011-11-05T06:26:57.040 回答