用clang和gcc编译嵌套函数 [英] compiling nested functions with clang versus gcc

查看:774
本文介绍了用clang和gcc编译嵌套函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ac文件,可以使用 GCC 来编译,没有任何问题:

gcc foo.c



然而,使用同一个文件我收到错误的定义函数使用 clang



clang foo.c

  foo:230:1:error:函数定义在这里是不允许的
{
^
foo.c:241: 1:错误:此处不允许使用函数定义
{
^
foo.c:253:1:错误:此处不允许使用函数定义

这些错误实例是代码主要部分内新函数的定义。我想知道为什么GCC不会被这个问题困扰,但clang呢?

解决方案

函数中定义的函数是C语言,由gcc实现。这是默认启用的。如果您将gcc编译为标准C编译器,如同 -ansi -pedantic -std = C99 或类似,也会抱怨嵌套的函数定义:

  xc:在函数'main'中:
xc:8:5:警告:ISO C禁止嵌套函数[-Wpedantic]
int嵌套(void)
^


I have a c file that I can compile with no problem using GCC like below:

gcc foo.c

however using the same file I am receiving error of having defined functions inside main using clang:

clang foo.c

foo:230:1: error: function definition is not allowed here
{
^
foo.c:241:1: error: function definition is not allowed here
{
^
foo.c:253:1: error: function definition is not allowed here

these instances of errors are the definitions of a new function inside the main section of the code. I want to know why GCC doesn't get bothered with this yet clang does?

解决方案

Functions defined within functions are an extension to the C language, implemented by gcc. This is enabled by default. If you make gcc a Standard C compiler, as with -ansi -pedantic or -std=C99 or similar, it will also complain about nested function definitions:

x.c: In function ‘main’:
x.c:8:5: warning: ISO C forbids nested functions [-Wpedantic]
     int nested(void)
     ^

这篇关于用clang和gcc编译嵌套函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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