_Noreturn in a c in c:error:expected specifier-qualifier-list before'_Noreturn'

Giz*_*zmo 4 c gcc c11 noreturn

我正在尝试编译一段包含_Noreturn的代码:

#ifndef SOMEHEADER_H
#define SOMEHEADER_H

#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>

extern struct s {

    _Noreturn void (*somenoreturnfunc)(bool);
} svar;

#endif
Run Code Online (Sandbox Code Playgroud)

这给了我:

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

所以我从这里尝试了这个建议:

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

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

但错误仍然发生:

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

我感到困惑,因为它是用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...
Run Code Online (Sandbox Code Playgroud)

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

它应该支持所有C11功能(从4.7开始支持_Noreturn ).

我做错了什么,我怎么能解决这个错误?

-
编辑:也许一个自包含的例子可以帮助:

main.c中:

#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>

struct s {

    _Noreturn void (*somenoreturnfunc)(bool);
} svar;

int main()
{
        svar.somenoreturnfunc = 0;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:

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

然而,当使用相同的命令行_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.
Run Code Online (Sandbox Code Playgroud)

M.M*_*M.M 5

在ISO C中,_Noreturn不能用于声明函数指针.它只能用于函数的声明.(参考:C11 6.7.4/2).

你将不得不放弃这个想法.函数说明符(inline_Noreturn)不是函数类型的一部分.

您的代码中还有另一个问题:您svar在标头中定义,如果标头包含在两个或多个翻译单元中,则会导致ODR违规.它应该只在标题中有一个声明,并在一个单元中有一个定义.


要通过函数指针调用并保留_Noreturn语义,也许你可以制作一个垫片,例如:

inline _Noreturn void call_somefunc( struct s *ps, bool b )
{
    ps->somenoreturnfunc(b);
}
Run Code Online (Sandbox Code Playgroud)

但是没有办法让编译器强制执行当你分配给函数指针时,它实际上是一个_Noreturn函数.