程序中的标识符会发生什么? [英] What happens to identifiers in a program?

查看:381
本文介绍了程序中的标识符会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手程序员。我只是想看到不同阶段的输出编译,组装&链接。我也不知道汇编语言。

I'm a novice programmer. I just wanted to see output at different phases compilation, assembling & linking. I don't know assembly language also.

我写了一个简单的程序

#include <stdio.h>

int humans = 9;

 int main() 
 {
        int lions = 2;
        int cubs = populate(lions);
        return 0;
 }

 int populate(int crappyVariable)
 {
    return ++crappyVariable;
}

我使用 gcc - S sample.c 我对汇编语言的输出感到惊讶。我失去了所有的变量名称&函数名。

I used gcc - S sample.c I'm surprised by the output of assembly language. I lost all the variable names & function names.

它保留了全局标识符,例如人类,填充,主体,但前缀为下划线_。所以,我不会把它当作使用标识符。无论如何,点是它丢失了所有的标识符。

it preserved the global identifiers like humans, populate, main but it prefixed them with underscores _. So, I wont considering it as using identifiers. Anyway, point is it lost all the identifiers.

我的问题是它将如何调用函数或引用变量?

My question is how would it call functions or refer to variables?

我真的很好奇输出的进一步阶段,这将是二进制的(这是不可见的)。

I'm really curious about further stages of output, which would be in binary (which is not viewable).

;之前链接?我想它会松动下划线前缀全局标识符吗?然后再问题是它如何调用函数或引用变量的操作?

How would be the output just after assembling & before linking? I guess it will loose even the underscore prefixed global identifiers too? Then again question is how would it call functions or refer to variables for operations?

我在互联网上搜索信息,但没有找到任何有用的。可能是我不知道要搜索什么。我不想读这本书的大书。但如果有任何文章,教程清楚的概念。这也是有帮助的。

I searched info on internet but couldn't find anything useful. May be I'm not sure what to search for. I don't want to read big books on this. But if there are any articles, tutorials which clear concepts. That would also be helpful.

我是一个新手程序员。所以,这是伟大的,你可以解释在简单但技术的术语。

I'm a novice programmer. So, it would be great you can explain in simple but technical terms.

编辑:作为回应,评论。我把我的问题分成了多个问题。这是此问题的第2部分:不清楚的工作链接器

In response, to the comment. I broke my question into multiple questions. Here is the 2nd part of this question: not clear with the job of the linker

推荐答案

在基本机器级别上,没有更多的名称,只是变量和代码的数字地址。因此,一旦你的代码被翻译成机器语言,这些名称就不再适用。

At the basic machine level, there are no more names, just numeric addresses for variables and code. Thus, once your code is translated to machine language, the names are gone for practical purposes.

如果你使用汇编器选项或反汇编代码编译,查看一些标识符;他们在那里帮助你找到你的方式在代码,因为你不希望在不必要的计算数据/代码偏移在你的头不必要的。

If you compile with a "to assembler" option or disassemble code, you may see some identifiers; they're there to help you find your way around the code, as you're not expected to be computing data/code offsets in your head unnecessarily.

回答你的关于链接的问题等:只有在C程序文件中使用的标签和标识符在程序编译为可重定位对象形式后才会消失。但是,需要外部定义的名称,例如 main(),因为外部模块将引用它们;因此编译的目标文件将包含一个小表格,列出外部可见的名称和它们引用的位置。

To answer your question about linking and such: Labels and identifiers that are only used "inside" a C program file are gone once the program is compiled to relocatable object form. However, externally defined names, such as main() are needed because external modules will reference them; so a compiled object file will contain a little table listing the externally visible names and which location they refer to. A linker can then patch together external references into your module from others (and vice versa) based on those names.

链接后,甚至不需要外部定义的名称任何更多。如果使用调试选项编译,名称表仍然可能附加到最终程序,因此您可以在调试程序时使用这些名称。

After linking, even the externally defined names aren't needed any more. If you compile with debug options, tables of names may still be attached to the final program, though, so you can use those names when debugging your program.

这篇关于程序中的标识符会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆