简体   繁体   English

GCC 会编译,但 clang 不会

[英]GCC will compile but clang wont

So in my finals there was an exercise asking what will the program print.所以在我的期末考试中有一个练习,询问程序会打印什么。

This is the code:这是代码:

#include <stdio.h>

int main (){
    int x=5, y=4;

    if (x>y);
      printf("A");

    if(x=4)
      printf("%d",x+y);

     return 0;
}

When I try to compile it using gcc -ansi -pedantic -Werror on a Debian machine, it compiles just fine and outputs "A8".当我尝试在 Debian 机器上使用gcc -ansi -pedantic -Werror编译它时,它编译得很好并输出“A8”。

However, when I try to compile it using clang -ansi -pedantic -Werror , I receive errors regarding the if (x=4) expression missing = and missing statement on if(x>y);但是,当我尝试使用clang -ansi -pedantic -Werror编译它时,我收到有关if (x=4)表达式缺少=if(x>y);上缺少语句的错误if(x>y); . .

Why does that happen, and which answer could be marked as correct?为什么会发生这种情况,哪个答案可以标记为正确?

compilers may emit different warnings.编译器可能会发出不同的警告。 For example clang warns about empty statement and gcc does not.例如, clang警告空语句,而gcc不会。

So if you set -Werror it will compile using gcc but will not compile using the clang.因此,如果您设置 -Werror,它将使用 gcc 进行编译,但不会使用 clang 进行编译。

BTW what is stopping you from reading the compiler messages.顺便说一句,是什么阻止了您阅读编译器消息。 They are self-explanatory.它们是不言自明的。 It even shows how to correct it.它甚至显示了如何纠正它。

#1 with x86-64 clang 9.0.0
<source>:6:9: error: if statement has empty body [-Werror,-Wempty-body]

if (x>y);

        ^

<source>:6:9: note: put the semicolon on a separate line to silence this warning

<source>:8:5: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]

if(x=4)

   ~^~

<source>:8:5: note: place parentheses around the assignment to silence this warning

if(x=4)

    ^

   (  )

<source>:8:5: note: use '==' to turn this assignment into an equality comparison

if(x=4)

    ^

    ==

<source>:12:2: error: no newline at end of file [-Werror,-Wnewline-eof]

}

 ^

3 errors generated.

Compiler returned: 1

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

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