简体   繁体   English

我是否需要来自GCC和C11的-dantic旗帜?

[英]Do I need -pedantic flag from GCC with C11?

I'm currently running Linux Mint on my Machine with GCC-5.3 because C11 is included default. 我目前正在使用GCC-5.3在我的机器上运行Linux Mint ,因为C11是默认包含的。

I started learning C for myself just for fun and at that time the GCC version was 4.8 if I remember right. 我开始为自己学习C只是为了好玩,当时GCC版本是4.8如果我没记错的话。

Any way if one use GCC-4.8 with -pedantic flag on the following program: 如果在下面的程序中使用GCC-4.8-pedantic标志,任何方式:

#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 . 在某种程度上清楚 - -pedantic标志是一个问题在这里所以我决定不使用它并避免它像避免-ansi因为新的(最后)标准C11

Now if I compile the same program with GCC-5.3 : 现在如果我用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: 现在基于以下问题在C和C ++中返回void类型,如果我尝试编译以下程序:

#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. 这告诉我,不知怎的,我确实需要-pedantic标志,但我并不痛。

So, my Question is, do we need to make use of -pedantic anymore with C11 ? 所以,我的问题是, 我们需要利用的 -pedantic 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. gcc的-pedantic选项告诉它严格执行您所请求的C标准规则。 This results in additional warning messages (or fatal errors if you use -pedantic-errors ). 这会导致其他警告消息(如果使用-pedantic-errors致命-pedantic-errors )。

The question is, do you want the compiler to warn you about code that violates the requirements of the C standard? 问题是,您是否希望编译器警告您违反C标准要求的代码?

If you want your code to be as portable as possible, use -pedantic and pay close attention to anything it tells you. 如果您希望您的代码尽可能便携,请使用-pedantic并密切关注它告诉您的任何内容。 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. 如果您希望代码依赖于非标准功能,请不要使用-pedantic - 但是您可能会面临使用不同编译器和/或不同目标系统编译代码的风险。

The specific messages you're run into are for things that have changed between C90 and C11. 您遇到的具体消息是针对C90和C11之间发生变化的事情。 C11 requires compilers to support at least 4095 characters in a string literal; C11要求编译器在字符串文字中支持至少4095个字符; 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. C90只需要509.(实际上,对于gcc,实际限制不是固定的,而是由编译时的可用内存强加的。标准中描述限制的方式并不那么简单,但我不会介入那个。)不过,你很少需要一个字符串文字。

C99 added the %zu format for printing a value of type size_t . C99添加了%zu格式,用于打印size_t类型的值。 If you want your code to be portable to pre-C99 implementations, you'll need to avoid using it; 如果您希望您的代码可以移植到C99之前的实现,那么您将需要避免使用它; for example, you can use printf("%lu\\n", (unsigned long)sizeof foo) . 例如,您可以使用printf("%lu\\n", (unsigned long)sizeof foo) In practice, most current implementations do support %zu . 实际上,大多数当前的实现都支持%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. 带有表达式的return语句,甚至是void类型的表达式,在使用void返回类型定义的函数中是不允许的。 That's a warning you should want (IMHO). 这是你应该要的警告(恕我直言)。

Bottom line: Use -pedantic if you want to strictly enforce the rules of the C standard. 底线:如果您想严格执行C标准的规则,请使用-pedantic If you don't want to do that, don't use -pedantic . 如果您不想这样做,请不要使用-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.) (但是除了警告你可能不关心之外,考虑至少偶尔使用-pedantic编译代码来清除它检测到的任何真实错误。)

The original C C89/C90 standard only required the compiler to allow string literals up to 509 bytes long. 最初的C C89 / C90标准只要求编译器允许字符串文字长达509个字节。 Code compiled to C90 standard using a string longer than 509 bytes long is not maximally portable; 使用长度超过509字节的字符串编译为C90标准的代码不是最大可移植的; a standard-conforming compiler could reject the code. 符合标准的编译器可以拒绝代码。 It is unlikely to be a problem in practice, but in theory that could happen. 它在实践中不太可能成为问题,但理论上可能会发生。

The limit was raised in C99 to 4095 bytes (and stayed the same in C11). 限制在C99中提高到4095字节(并且在C11中保持不变)。 Consequently, you have to have a much longer string to run foul of the limit with C99 or C11. 因此,您必须使用更长的字符串来使用C99或C11来限制极限。

The GCC 4.x compilers worked with the C90 standard by default. GCC 4.x编译器默认使用C90标准。 The GCC 5.x compilers work with the C11 standard by default. GCC 5.x编译器默认使用C11标准。 Thus, code that is not maximally portable to C90 will generate warnings when compiled with -pedantic under GCC 4.x, but won't generate the same warnings with GCC 5.x unless the construct is also not portable to all C11 compilers — unless it violates one of the C11 compile-time limits too. 因此,在GCC 4.x下使用-pedantic进行编译时,不能最大程度地移植到C90的代码将生成警告,但不会生成与GCC 5.x相同的警告,除非该构造也不能移植到所有C11编译器 - 除非它也违反了C11编译时限制之一。

The -pedantic flag has its uses. -pedantic标志有其用途。 For example, yesterday, someone was running into problems because they were using: 例如,昨天有人遇到问题因为他们正在使用:

void *p = malloc(sizeof(*p));

That's malformed code according to the standard, but GCC (5.3.0 specifically tested, but the other 5.x and 4.x versions behave the same) allows it, interpreting sizeof(*p) as being 1 . 根据标准,这是错误的代码,但GCC(5.3.0专门测试,但其他5.x和4.x版本表现相同)允许它,将sizeof(*p)解释为1 That's not portable to other compilers. 这对其他编译器来说是不可移植的。 Using -pedantic reports the problem; 使用-pedantic报告问题; not using -pedantic does not. 不使用-pedantic不。

From the formal point of view, if you are planning to write your code in standard C you definitely need -pedantic flag (moreover, -pedantic-errors might be even better idea). 从正式的角度来看,如果你打算用标准C编写你的代码,你肯定需要-pedantic标志(而且, -pedantic-errors可能是更好的主意)。 However, the original implementation of -pedantic suffered from one rather questionable design decision: it included warnings related to implementation limits (which is OK), and on top of that it also turned them into errors in -pedantic-errors mode (which, in my opinion, is unacceptable). 然而, -pedantic的最初实现遭受了一个相当可疑的设计决策:它包括与实现限制相关的警告(这是正常的),并且最重要的是它也将它们变成了错误的-pedantic-errors模式(其中,我的意见,是不可接受的)。

Warnings about implementation limits might be useful, but still it might be a good idea to keep them controllable independently, keeping -pedantic reserved for straightforward constraint violations. 关于实现限制的警告可能是有用的,但仍然可以保持它们独立可控,保持-pedantic保留直接的约束违规。

The fact that you no longer see warnings about implementation limits with -pedantic might mean that GCC 5 finally decided to take care of this matter. 您不再看到有关实施限制的警告, -pedantic可能意味着GCC 5最终决定处理此问题。 If so, it would be a welcome change (but more likely it is the limits that changed). 如果是这样,那将是一个值得欢迎的改变(但更可能是改变的限制)。

-pedantic is just a flag that turns on a whole bunch of warnings and errors and you can use it if you want, but it sounds like you really aren't using c11 or else it wouldn't give you that particular warning... -pedantic只是一个标志,可以打开一大堆警告和错误,如果你愿意,你可以使用它,但听起来你真的没有使用c11,否则它不会给你那个特别的警告......

try: 尝试:

gcc -std=c11 -Wall -pedantic program.c -o program

that will make pre gcc-5 version use the C11 std as default rather than gnu89 这将使pre gcc-5版本使用C11 std作为默认值而不是gnu89

The default mode for C is now -std=gnu11 instead of -std=gnu89

from gcc.gnu.org/gcc-5/changes.html 来自gcc.gnu.org/gcc-5/changes.html

and to go into a little more detail: the difference between c11 and gnu11 is subtle, I haven't looked into c11 as much, but in c99/gnu99 the relationship was the gnu99 was a superset of c11 and allowed some compiler extensions to the language... I highly suspect this is the same relationship with c11/gnu11 并且更详细一点:c11和gnu11之间的区别是微妙的,我没有仔细研究c11,但在c99 / gnu99中,关系是gnu99是c11的超集,并允许一些编译器扩展到语言...我非常怀疑这与c11 / gnu11的关系是一样的

That [-Wpedantic] at the end of the error message means that the warning is generated by -pedantic compiler option. 错误消息末尾的[-Wpedantic]表示警告是由-pedantic编译器选项生成的。 In other words -pedantic is already enabled. 换句话说, -pedantic已经启用。

You do not need the -pedantic flag. 你不需要-pedantic标志。 Almost nobody needs the -pedantic flag. 几乎没有人需要-pedantic旗帜。

Pedantry - Excessive concern with minor details and rules. Pedantry - 过度关注细节和规则。

-pedantic warnings can be ignored, practically by definition. -pedantic的警告可以忽略不计,几乎从定义。 The flag may be useful when writing cross-platform code but that's it. 在编写跨平台代码时,该标志可能很有用,但就是这样。

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

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