代码之家  ›  专栏  ›  技术社区  ›  Gizmo

_ C中结构中的noreturn:错误:在“noreturn”之前需要说明符限定符列表

  •  4
  • Gizmo  · 技术社区  · 7 年前

    我试图编译一段代码,其中包含:

    #ifndef SOMEHEADER_H
    #define SOMEHEADER_H
    
    #include <stdalign.h>
    #include <stdbool.h>
    #include <stdint.h>
    
    extern struct s {
    
        _Noreturn void (*somenoreturnfunc)(bool);
    } svar;
    
    #endif
    

    这给了我:

    error: expected specifier-qualifier-list before '_Noreturn' 关于: _Noreturn void (*somenoreturnfunc)(bool);

    所以我尝试了 here :

    #ifndef SOMEHEADER_H
    #define SOMEHEADER_H
    
    #include <stdalign.h>
    #include <stdbool.h>
    #include <stdint.h>
    
    #include "noreturn.h"
    
    extern struct s {
    
        noreturn void (*somenoreturnfunc)(bool);
    } svar;
    
    #endif
    

    Neururn.H:

    #ifndef NO_RETURN_H
    #define NO_RETURN_H
    #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
    #define noreturn _Noreturn
    #elif defined(__GNUC__)
    #define noreturn __attribute__((noreturn))
    #else
    #define noreturn
    #endif
    #endif
    

    但错误仍然会发生:

    In file included from ../include/someinclude.h:8:0,
                     from src/main.c:17:
    ../include/noreturn.h:4:18: error: expected specifier-qualifier-list before '_Noreturn'
     #define noreturn _Noreturn
                      ^
    ../include/someinclude.h:19:5: note: in expansion of macro 'noreturn'
         noreturn void (*somenoreturnfunc)(bool);
         ^
    

    我很困惑,因为它是用c11编译的,所以应该可以工作:

    make V=1
    cc src/main.c
    arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -std=c11 -Wall -pedantic -ffreestanding -static -I../libopencm3/include -I../include -DSTM32F1 -g -DDEBUG -DBUILD_STYLE=\"DEBUG\" -O0 -Iinclude -I../librt/include  -MMD -MT build/main.o -MF build/main.d -o build/main.o -c src/main.c
    In file included...
    

    GCC版本为5.4.1:

    arm-none-eabi-gcc --version
    arm-none-eabi-gcc (15:5.4.1+svn241155-1) 5.4.1 20160919
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

    它应该支持所有的C11功能(_noreturn被支持 since 4.7 )

    我做错了什么?如何解决此错误?

    ——
    编辑:也许一个独立的例子可以帮助:

    主要内容:

    #include <stdalign.h>
    #include <stdbool.h>
    #include <stdint.h>
    
    struct s {
    
        _Noreturn void (*somenoreturnfunc)(bool);
    } svar;
    
    int main()
    {
            svar.somenoreturnfunc = 0;
            return 0;
    }
    

    编译如下:

    arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -std=c11 -Wall -pedantic -ffreestanding -static -DSTM32F1 -g -DDEBUG -DBUILD_STYLE=\"DEBUG\" -O0 -MMD -MT main.o -MF main.d -o main.o -c main.c
    main.c:7:5: error: expected specifier-qualifier-list before '_Noreturn'
         _Noreturn void (*somenoreturnfunc)(bool);
         ^
    main.c:5:8: warning: struct has no members [-Wpedantic]
     struct s {
            ^
    main.c: In function 'main':
    main.c:12:6: error: 'struct s' has no member named 'somenoreturnfunc'
      svar.somenoreturnfunc = 0;
          ^
    

    但是,当使用相同的命令行编译时 _Noreturn 编译成功。

    使用编译时也会发生这种情况 gcc -std=c11 -o main main.c :

    $ gcc -std=c11 -o main main.c
    main.c:7:5: error: expected specifier-qualifier-list before ‘_Noreturn’
         _Noreturn void (*somenoreturnfunc)(bool);
         ^~~~~~~~~
    main.c: In function ‘main’:
    main.c:12:6: error: ‘struct s’ has no member named ‘somenoreturnfunc’
      svar.somenoreturnfunc = 0;
          ^
    $ gcc --version
    gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
    Copyright (C) 2016 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    3 回复  |  直到 7 年前
        1
  •  5
  •   M.M    7 年前

    _Noreturn

    inline

    svar


    inline _Noreturn void call_somefunc( struct s *ps, bool b )
    {
        ps->somenoreturnfunc(b);
    }
    

        2
  •  2
  •   dpi    7 年前

    _Noreturn inline

        3
  •  0
  •   Paul Ogilvie    7 年前