通过预处理、编译、汇编和链接进行调试

发布于 2024-08-17 03:36:24 字数 243 浏览 0 评论 0原文

有时,当gcc输出错误时,我们会通过-E、-S、-c等选项来分解预处理、编译、汇编、链接各个阶段的过程。这是示例

我只是想知道这四个阶段中的每一个阶段都可能发生什么类型的错误,如果其中一个阶段发生了一个错误,而之前的阶段没有发生,这将如何指导您找到错误的原因并纠正它?

Sometimes when there are errors output by gcc, one will break down the the process for each stage of preprocessing, compilation, assembly, linking by options like -E, -S and -c. Here is an example.

I just wonder what types of errors could happen at each of these four stages and if there is one error occuring at one of these stages but not at previous stages, how will this guide you to locate the reason of the error and correct it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2024-08-24 03:36:24

如果我清楚地记得每个术语的含义:

  • 预处理是解释/执行所有以 # 开头的行的事实,如果后面的内容未被识别为具有有效的语法,则会报告为错误;
  • 编译是将源代码翻译成目标代码的过程;如果您不尊重该语言的语法,则会报告错误;
  • 链接是获取一个或多个目标文件并从中创建一个可执行文件的事实,从而验证声明和使用的所有内容都已定义。

预处理错误:“abcdefg”不是有效的关键字,因此预处理将失败:

#abcdefg

编译错误:“fight!now”不是有效的标识符,因此编译将失败:

int fight!now;

链接错误:“myfunc”由从未定义过声明:

extern int myfunc();

int main() {
    return myfunc();
}

你看,知道工具链检测到错误的位置有助于了解可能是什么类型的错误。但通常工具链发出的消息足以了解错误所在的位置。当然,它需要习惯于将参数传递为 -Wall 甚至 -Wextra 以获得更多有关可能出现问题的警告。

If I remember well the meaning of each term:

  • preprocessing is the fact of interpreting/executing all lines which begin by # and if what follows is not recognized as having a valid syntax, it will be reported as an error;
  • compilation is the fact of translating the source code to object code; if you don't respect the syntax of the language, an error will be reported;
  • linking is the fact of taking one or multiple object files and creating one executable file out of them, thus verifying that everything which was declared and used is also defined.

Preprocessing error: `abcdefg' is not a valid keyword so preprocessing will fail:

#abcdefg

Compilation error: `fight!now' is not a valid identifier so compilation will fail:

int fight!now;

Linking error: `myfunc' is declared by never defined:

extern int myfunc();

int main() {
    return myfunc();
}

You see, knowing where the tool chain detects the error helps knowing what kind of error it could be. But often the messages the tool chain emits is enough to understand where the error resides. Of course it requires being used to passing parameters as -Wall or even -Wextra to get more warnings about what could be wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文