C语言 GCC 中宏中函数的无效存储类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37445800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:26:00  来源:igfitidea点击:

Invalid storage class for a function in a macro in GCC

cgccmacros

提问by feos

I have this legacy macro

我有这个遗留宏

#define ASYNC_FUNCTION(x)                                               \
void x(void) {                                                          \
    static void internal ## x (ASYNCIOCB *);                            \
    ASYNCIOCB *aiocb = AcquireAsyncIOCB();                              \
    CallAsyncNativeFunction_md(aiocb, internal ## x);                   \
}                                                                       \
static void internal ## x (ASYNCIOCB *aiocb)

followed by this one

其次是这个

#define ASYNC_FUNCTION_START(x)  ASYNC_FUNCTION(x) {
#define ASYNC_FUNCTION_END       ASYNC_resumeThread(); }

And their use looks like this:

它们的用法如下所示:

ASYNC_FUNCTION_START(Occasional_function_name)
{
    //actual stuff    
}
ASYNC_FUNCTION_END

It compiles fine with cl, but gcc gives

它用 cl 编译得很好,但 gcc 给出

invalid storage class for function ‘internalOccasional_function_name'
     static void internal##x (ASYNCIOCB *);    
                 ^

I tried to expand them all just to see what it becomes and found nothing broken. I also searched for unclosed curved brackets in the file, and found other macros like this

我试图将它们全部展开只是为了看看它变成了什么,但没有发现任何损坏。我还搜索了文件中未闭合的弧形括号,并找到了其他类似这样的宏

#define Foo_Bar1()  {                                                \
  extern int foo;                                                    \
  int bar = foo;                                                     \
  if (condition) {                                                   \
    Bar_Foo();                                                       \
  }                                                                  \

#define Foo_Bar2()                                                   \
  if (condibar != footion1){                                         \
    AbortAsyncIOCB(aiocb);                                           \
    return;                                                          \
  }                                                                  \
  if (condition) {                                                   \
    Bar_Foo();                                                       \
  }                                                                  \
}

No other headers are included, so other than that last macro looking weird, I couldn't find any obvious errors. I'm using cygwin and I'm fairly clueless.

没有包含其他标题,所以除了最后一个看起来很奇怪的宏之外,我找不到任何明显的错误。我正在使用 cygwin 并且我相当无能为力。

回答by dbush

You can't declare a static function inside of another one. You need to put the declaration outside of void(x):

你不能在另一个内部声明一个静态函数。您需要将声明放在 之外void(x)

#define ASYNC_FUNCTION(x)                                               \
static void internal ## x (ASYNCIOCB *);                                \
void x(void) {                                                          \
    ASYNCIOCB *aiocb = AcquireAsyncIOCB();                              \
    CallAsyncNativeFunction_md(aiocb, internal ## x);                   \
}                                                                       \
static void internal ## x (ASYNCIOCB *aiocb)

Then, if you run the source through just the preprocessor via gcc -E, you'll get something like this (extra spacing added):

然后,如果您只通过预处理器 via 运行源代码gcc -E,您将得到如下结果(添加额外的间距):

static void internalOccasional_function_name (ASYNCIOCB *);
void Occasional_function_name(void)
{
    ASYNCIOCB *aiocb = AcquireAsyncIOCB();
    CallAsyncNativeFunction_md(aiocb, internalOccasional_function_name);
}
static void internalOccasional_function_name (ASYNCIOCB *aiocb) 
{
    {
        int a=1;
    }
    ASYNC_resumeThread();
}

回答by AnT

In C language local function declarations can optionally include storage class specifier extern. That's the only storage class specifier a local function declaration may have. No other storage class specifiers are allowed.

在 C 语言中,局部函数声明可以选择包含存储类说明符extern。这是局部函数声明可能具有的唯一存储类说明符。不允许使用其他存储类说明符。

6.7.1/7:

6.7.1/7:

The declaration of an identifier for a function that has block scope shall have no explicit storage-class specifier other than extern.

具有块作用域的函数的标识符声明除extern 外不应有显式的存储类说明符。