为什么使用linux开发项目,我的Linux开发项目使用Clang vs GCC

小编典典

编辑:

海湾合作委员会的成员确实改善了海湾合作委员会(啊竞争)的诊断经验。他们创建了一个Wiki页面在此处进行展示。gcc

4.8现在也具有很好的诊断功能(gcc 4.9x添加了颜色支持)。Clang仍然处于领先地位,但差距正在缩小。

原版的:

对于学生,我会无条件推荐Clang。

现在尚不清楚在gcc和Clang之间生成代码的性能(尽管我认为gcc 4.7仍然处于领先地位,我还没有最终的基准),但是对于学生来说,学习实际上并不重要。

另一方面,对于初学者来说,Clang极其清晰的诊断无疑更容易理解。

考虑以下简单代码段:

#include

#include

struct Student {

std::string surname;

std::string givenname;

}

std::ostream& operator<

return out << "{" << s.surname << ", " << s.givenname << "}";

}

int main() {

Student me = { "Doe", "John" };

std::cout << me << "\n";

}

您会立即注意到,在定义了Student类之后,分号就消失了,对:)吗?

prog.cpp:9: error: expected initializer before ‘&’ token

prog.cpp: In function ‘int main()’:

prog.cpp:15: error: no match for ‘operator<

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:112: note: candidates are: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<& (*)(std::basic_ostream<_chart _traits>&)) [with _CharT = char, _Traits = std::char_traits]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:121: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<& (*)(std::basic_ios<_chart _traits>&)) [with _CharT = char, _Traits = std::char_traits]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:131: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:169: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:173: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:177: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/ostream.tcc:97: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:184: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/ostream.tcc:111: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:195: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:204: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:208: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:213: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:217: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:225: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:229: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<]

/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/ostream.tcc:125: note: std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator<*) [with _CharT = char, _Traits = std::char_traits]

Clang也不是这里的主角,但仍然:

/tmp/webcompile/_25327_1.cc:9:6: error: redefinition of 'ostream' as different kind of symbol

std::ostream& operator<

^

In file included from /tmp/webcompile/_25327_1.cc:1:

In file included from /usr/include/c++/4.3/string:49:

In file included from /usr/include/c++/4.3/bits/localefwd.h:47:

/usr/include/c++/4.3/iosfwd:134:33: note: previous definition is here

typedef basic_ostream ostream; ///< @isiosfwd

^

/tmp/webcompile/_25327_1.cc:9:13: error: expected ';' after top level declarator

std::ostream& operator<

^

;

2 errors generated.

我故意选择一个示例,该示例会触发一个不清楚的错误消息(来自语法上的歧义),而不是典型的“哦,我的上帝Clang读了我的思想”示例。尽管如此,我们注意到Clang避免了大量错误。无需将学生吓跑。

2020-06-07

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值