【问题标题】:GCC will compile but clang wontGCC 将编译但 clang 不会
【发布时间】:2020-01-10 16:44:04
【问题描述】:

所以在我的期末考试中,有一个练习询问程序将打印什么。

这是代码:

#include <stdio.h>

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

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

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

     return 0;
}

当我尝试在 Debian 机器上使用 gcc -ansi -pedantic -Werror 编译它时,它编译得很好并输出“A8”。

但是,当我尝试使用 clang -ansi -pedantic -Werror 编译它时,我收到关于 if (x=4) 表达式缺少 = 和缺少 if(x&gt;y); 上的语句的错误。

为什么会发生这种情况,哪个答案可以被标记为正确?

【问题讨论】:

  • === 不是同一个运算符。
  • 是的,我明白了。我不明白为什么它在 GCC 中编译而在 clang 中没有。
  • 显然gcc 相信你知道自己在做什么,而clang 不知道:)
  • if (x&gt;y); printf("A"); 等于 if (x&gt;y) { /* empty block */ } printf("A");
  • gcc -Wall -Wextra -Wpedantic 有什么不同吗?

标签: c gcc clang


【解决方案1】:

编译器可能会发出不同的警告。例如,clang 警告空语句,gcc 不会。

所以如果你设置 -Werror 它将使用 gcc 编译但不会使用 clang 编译。

顺便说一句,是什么阻止了您阅读编译器消息。它们是不言自明的。它甚至显示了如何纠正它。

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-03
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2017-10-12
    相关资源
    最近更新 更多