您的位置:首页 > 其它

关于gcc的一点小人性化提示

2014-10-27 05:44 302 查看
现在对于大多数平台的C编译器来说都会有很多种选择,而gcc和clang无疑是2个非常优秀的C编译器。当然他们也不只是C编译器。我最近用clang的比较多,原因有很多。不过一些小的细节很让我喜欢,比如OS X系统中,clang的编译器警告或错误提示是以彩色文本醒目打印出来的。

而gcc则无论如何打印的颜色都一样(我不知道是否有什么设置可以改变这一点)。

但是gcc也有其人性化的一点,就是在某些比较隐晦的错误时,会有更友好的提示。比如标签后不能直接写声明这种情况,2种编译器的结果如下:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <unistd.h>

int main(void)
{
	int i = 0;
	puts("hello world!");
LAB:
	int x = 10;
	printf("now is %d\n",i++);
	sleep(3);
	goto LAB;
	return 0;
}


clang编译结果:

apple@kissAir: c_src$clang -std=c11 -Wall -O3 -g0 -o x x.c

x.c:12:2: error: expected expression

int x = 10;

^

1 error generated.

gcc编译结果:

apple@kissAir: c_src$gcc -std=c11 -Wall -O3 -g0 -o x x.c

gcc: warning: couldn't understand kern.osversion '14.0.0

x.c: In function 'main':

x.c:12:2: error: a label can only be part of a statement and a declaration is not a statement

int x = 10;

^

x.c:12:6: warning: unused variable 'x' [-Wunused-variable]

int x = 10;

^

可以很清楚的看出,前者只是简单一句模凌两可的提示,让初学者丈二和尚摸不着头脑;不过后者解释的就很清楚了,这是不是让初学乍到的程序猿们感觉很温馨呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: