GCC中宏中的函数的存储类无效

feo*_*eos 1 c macros gcc

我有这个传统的宏

#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)
Run Code Online (Sandbox Code Playgroud)

接着是这一个

#define ASYNC_FUNCTION_START(x)  ASYNC_FUNCTION(x) {
#define ASYNC_FUNCTION_END       ASYNC_resumeThread(); }
Run Code Online (Sandbox Code Playgroud)

它们的用途如下:

ASYNC_FUNCTION_START(Occasional_function_name)
{
    //actual stuff    
}
ASYNC_FUNCTION_END
Run Code Online (Sandbox Code Playgroud)

它与cl编译很好,但是gcc给出了

invalid storage class for function ‘internalOccasional_function_name’
     static void internal##x (ASYNCIOCB *);    
                 ^
Run Code Online (Sandbox Code Playgroud)

我试图扩展它们只是为了看它变成什么并且没有发现任何损坏.我还在文件中搜索了未闭合的弧形括号,并找到了这样的其他宏

#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();                                                       \
  }                                                                  \
}
Run Code Online (Sandbox Code Playgroud)

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

dbu*_*ush 6

您不能在另一个内部声明静态函数.您需要将声明置于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)
Run Code Online (Sandbox Code Playgroud)

然后,如果你只通过预处理器来运行源代码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();
}
Run Code Online (Sandbox Code Playgroud)

  • “你不能在另一个函数中声明一个函数”。呃……是的,当然可以。为什么不?只是不允许 `static` 函数声明。 (2认同)