我是否需要使用C11的GCC旗号? [英] Do I need -pedantic flag from GCC with C11?

查看:162
本文介绍了我是否需要使用C11的GCC旗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的机器上运行 Linux Mint 并使用 GCC-5.3 ,因为 C11 是包含的默认值。



我开始为自己学习 C 那时 GCC 版本是 4.8 ,如果我没有记错的话。



如果在下面的程序中使用 GCC-4.8 -pedantic 标志,任何方式都可以:

  #include< stdio.h> 
#include< string.h>

INT主(无效){
字符* ARR = 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890;
size_t length = strlen(arr);
printf(Arr =%zu\\\
的长度,长度);
}

在编译时,会得到以下警告:

  program.c:在函数'main'中:
program.c:5:5:warning:字符串长度'510'大于'的长度509' ISO C90的编译器需要支持[-Woverlength串]
字符* ARR = 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890;
$
program.c:7:5:warning:ISO C90不支持'z'gnu_printf长度修饰符[-Wformat =]
printf(Arr =%zu\\的长度\
,长度);
$
program.c:8:1:warning:控制到达非void函数的结尾[-Wreturn-type]
}
^

如果我们看到这部分警告:

  warning:字符串长度'510'大于'509'长度ISO C90编译器需要支持[-Woverlength-strings] 

不知何故, -pedantic 标志在这里是一个问题,所以我决定不使用它,避免它像避免 -ansi 也是因为新的(最后一个)标准 C11



<现在如果我使用 GCC-5.3 编译相同的程序:

  gcc-5 -Wall -pedantic program.c -o程序

该程序编译良好,没有警告。



现在基于以下问题如果我尝试编译fol,则返回C和C ++中的void类型 lowing program:

  #include  
#include< string.h>

void f(void);
void f2(void);

int main(void){
f();



void f(void){
}

void f2(void){
return f() ;
}

如下:

  gcc-5 -Wall -pedantic program.c -o program 

我得到:

  program.c:在函数'f2'中:
program.c:16 :16:warning:ISO C在表达式中禁止'返回',返回void [-Wpedantic]
return f();
^

但是编译没有'-antantic`标志。这对我来说是一个混乱。



这说明我确实需要 -pedantic 标志,但是我'm not sore。



所以,我的问题是,我们是否需要利用 -pedantic

code> with C11

解决方案

如果你需要它,你需要它。如果你不这样做,你就不会。



gcc的 -pedantic 选项告诉它严格执行规则您要求的C标准。这会产生额外的警告消息(或者如果使用 -pedantic-errors )致命错误。)



问题是,你是否希望编译器警告你有关违反C标准要求的代码?



如果您希望代码尽可能便携,请使用 -pedantic 并密切关注它告诉你的任何事情。如果你希望你的代码依赖于非标准的功能,不要使用 -pedantic - 但是你会冒着代码可能无法用另一个编译器编译的风险和/或针对不同的目标系统。

您遇到的特定消息是针对C90和C11之间发生变化的事情。 C11要求编译器在字符串文字中支持至少4095个字符; C90只需要509.(实际上,对于gcc来说,实际的限制并不是固定的,而是由编译时的可用内存强加的。标准中描述的限制方式并不是那么简单,但我不会进入)。然而,你很少需要一个字符串字符串。



C99添加了%zu 用于打印 size_t 类型值的格式。如果你希望你的代码可以移植到C99之前的实现中,你需要避免使用它;例如,您可以使用 printf(%lu \ n,(unsigned long)sizeof foo)。实际上,大多数当前实现都支持%zu



A return void 语句,甚至是 void 类型的表达式c>返回类型。底线:如果您想严格执行,请使用 -pedantic 这是您应该需要的警告(恕我直言)。

C标准的规则。如果你不想这样做,不要使用 -pedantic 。 (但是考虑到编译你的代码至少偶尔会清除它检测到的实际错误,除了你可能不关心的警告之外,还有 -pedantic )。


I'm currently running Linux Mint on my Machine with GCC-5.3 because C11 is included default.

I started learning C for myself just for fun and at that time the GCC version was 4.8 if I remember right.

Any way if one use GCC-4.8 with -pedantic flag on the following program:

#include <stdio.h>
#include <string.h>

int main(void){
    char *arr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    size_t length = strlen(arr);
    printf("Length of Arr = %zu\n",length);
}

At the compile time one gets the following warnings:

program.c: In function ‘main’:
program.c:5:5: warning: string length ‘510’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]
     char *arr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
     ^
program.c:7:5: warning: ISO C90 does not support the ‘z’ gnu_printf length modifier [-Wformat=]
     printf("Length of Arr = %zu\n",length);
     ^
program.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

If we see this part of the warning:

warning: string length ‘510’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]

Is somehow clear that -pedantic flag is a problem here so I decided to not use it and avoid it like avoid -ansi too because of the new (last) standard C11.

Now if I compile the same program with GCC-5.3:

gcc-5 -Wall -pedantic program.c -o program

The program compiles fine with NO WARNINGS.

Now based on the following Question Return void type in C and C++ if I try to compile the following program:

#include <stdio.h>
#include <string.h>

void f(void);
void f2(void);

int main(void){
    f();
}


void f(void){
}

void f2(void){
    return f();
}

With the following:

gcc-5 -Wall -pedantic program.c -o program

I get:

program.c: In function ‘f2’:
program.c:16:16: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
         return f();
                ^

But compiles fine without the ´-pedantic` flag. This is a confusion for me.

Which shows me that somehow I do need the -pedantic flag, but I'm not sore.

So, my Question is, do we need to make use of -pedantic anymore with C11?

解决方案

If you need it, you need it. If you don't, you don't.

gcc's -pedantic option tells it to strictly enforce the rules of the C standard you've requested. This results in additional warning messages (or fatal errors if you use -pedantic-errors).

The question is, do you want the compiler to warn you about code that violates the requirements of the C standard?

If you want your code to be as portable as possible, use -pedantic and pay close attention to anything it tells you. If you want your code to depend on non-standard features, don't use -pedantic -- but then you run the risk that your code might not compile with a different compiler and/or for a different target system.

The specific messages you're run into are for things that have changed between C90 and C11. C11 requires compilers to support at least 4095 characters in a string literal; C90 only requires 509. (In practice, for gcc, the actual limit is not fixed but is imposed by available memory at compile time. And the way the limits are described in the standard is not that simple, but I won't get into that.) Still, you'll rarely need to have a string literal that long.

C99 added the %zu format for printing a value of type size_t. If you want your code to be portable to pre-C99 implementations, you'll need to avoid using it; for example, you can use printf("%lu\n", (unsigned long)sizeof foo). In practice, most current implementations do support %zu.

A return statement with an expression, even an expression of type void, is not permitted in a function defined with a void return type. That's a warning you should want (IMHO).

Bottom line: Use -pedantic if you want to strictly enforce the rules of the C standard. If you don't want to do that, don't use -pedantic. (But consider compiling your code with -pedantic at least occasionally to weed out any real errors it detects, in addition to warnings that you might not care about.)

这篇关于我是否需要使用C11的GCC旗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆