0

I am using gcc to compile my C code. Just out of curiosity I would like to know at exactly which stage did the error occur. Is there a way to print this information when compilation error occurs?

4

2 回答 2

2

正如上面所评论的那样,如果您理解该消息,那么确定消息来自何处(预处理器、汇编器、编译器或链接器)应该没有问题。但是,要回答您的问题,这三个阶段可以通过传递给gcc.

您可能会尝试的第一个选项是-E. 通过-E将在预处理阶段后停止gcc,输出只是预处理后的 C 代码。如果您的错误消息出现在那里,它是由预处理器生成的。否则,请继续阅读。

第二个选项是-S。这将在预处理和编译之后但在组装和链接之前停止 GCC。如果您在此阶段看到错误消息,则它是由预处理器(但您已在上一步中排除)或编译器生成的。否则,请继续阅读。

尝试的第三个也是最后一个选项是-c. 通过-c会要求 GCC 对源文件进行预处理、编译和汇编,但不要链接。如果您在此阶段看到错误消息,则它是由预处理器或编译器(但您已在上一步中排除了那些)或汇编器生成的。否则它来自链接器。

于 2013-06-17T09:13:38.930 回答
1

这已经发生了。

#ifedf THIS
    that;
#Endif

$gcc ifed.c
ifed.c:1:2: error: invalid preprocessing directive #ifedf
ifed.c:2:5: warning: data definition has no type or storage class
ifed.c:3:2: error: invalid preprocessing directive #Endif

int main() {
    int int, int;
}

$ gcc mai.c
mai.c: In function ‘main’:
mai.c:2:9: error: two or more data types in declaration specifiers
mai.c:2:12: error: expected identifier or ‘(’ before ‘,’ token

int main() {
    bogusfunction();
}

gcc lin.c
/tmp/ccUc0WAL.o:lin.c:(.text+0xc): undefined reference to `_bogusfunction'
collect2: ld returned 1 exit status
于 2013-06-17T09:04:57.937 回答