强制编译为C99标准 [英] Forcing compiler to C99 standard

查看:1138
本文介绍了强制编译为C99标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目上编码,当我发现匿名结构我已经使用了一段时间实际上只有在C11,而不是C99,我想要编码的标准。



给定以下代码:

  struct data {
int a;
struct {
int b;
int c;
};
};

int main()
{
struct data d;

d.a = 0;
d.b = 1;
d.c = 2;
return 0;
}

此代码只能在C11中编译(或者如果编译器扩展提供此功能,启用)。让我们来看看不同编译器上的结果:



clang 5



 编译器:
Apple LLVM版本5.0(clang-500.2.79)(基于LLVM 3.3svn)
目标:x86_64-apple-darwin13.1.0
线程模型:posix

clang -std = c99 -Wall test.c -o test
result:
** OK **



gcc 4.1



 编译器:
gcc(GCC) 2 20080704(Red Hat 4.1.2-54)
命令:
gcc -std = c99 -Wall test.c -o test
result:
** NOT OK **
test.c:6:warning:声明不声明任何东西
test.c:在函数'main'中:
test.c:14:error:'struct data'没有成员命名为'b'
test.c:15:错误:'struct data'没有成员命名为'c'


b $ b

gcc 4.7



 编译器:
gcc(GCC)4.7.2 20121109 .2-8)
命令:
gcc -std = c99 -Wall test.c -o test
result:
** OK **
test.c :在函数'main'中:
test.c:11:14:warning:变量'd'设置但未使用[-Wunused-but-set-variable]
-std = c99 ,我一直试图强制编译器进入C99模式,但显然这不工作(除了gcc 4.1编译精细没有-std参数)。
现在我的问题是,如果我编写的代码不符合我使用 -std指定的标准,我如何强制编译器gcc和clang,在任何版本中发出错误?是否有一些我不知道的命令行参数?

解决方案

-std = c99 不会禁用语言扩展(GNU C在C99中有anon结构)。



-pedantic (或者 -pedantic-错误 )标志使编译器对语言扩展发出警告。


I was coding on my project when I discovered that the anonymous structs I've been using for a while are actually only available in C11, not C99, the standard I want to code against.

Given the following code:

struct data {
    int a;
    struct {
        int b;
        int c;
    };
};

int main()
{
    struct data d;

    d.a = 0;
    d.b = 1;
    d.c = 2;
    return 0;
}

This code should only compile in C11 (or if compiler extensions provide this feature and are enabled). So let's see the results on different compilers:

clang 5

compiler:
    Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
    Target: x86_64-apple-darwin13.1.0
    Thread model: posix
command: 
    clang -std=c99 -Wall test.c -o test
result: 
    **OK**

gcc 4.1

compiler:
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
command: 
    gcc -std=c99 -Wall test.c -o test
result: 
    **NOT OK**
    test.c:6: warning: declaration does not declare anything
    test.c: In function 'main':
    test.c:14: error: 'struct data' has no member named 'b'
    test.c:15: error: 'struct data' has no member named 'c'

gcc 4.7

compiler:
    gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
command: 
    gcc -std=c99 -Wall test.c -o test
result: 
    **OK**
    test.c: In function 'main':
    test.c:11:14: warning: variable 'd' set but not used [-Wunused-but-set-variable]

I've always tried to force the compiler to C99 mode by specifying -std=c99, but obviously this doesn't work (except for gcc 4.1 which compiles fine without the -std parameter). So my question is now, how can I force the compilers gcc and clang, in any version, to issue an error if I write code that does not conform to the standard I specify using -std? Is there some command line argument that I don't know of?

解决方案

-std=c99 won't disable language extensions (GNU C has anon structs in C99).

The -pedantic (or -pedantic-errors) flags make the compiler warn on language extensions.

这篇关于强制编译为C99标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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