首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Watcom C中定义NORETURN宏

如何在Watcom C中定义NORETURN宏
EN

Stack Overflow用户
提问于 2019-08-18 05:45:39
回答 1查看 198关注 0票数 3

是否可以使用Open Watcom C编译器(而不是C++)来执行#define NORETURN(x)宏?

基于the fine manual,我有这个文件test.c

代码语言:javascript
复制
#include <stdlib.h>

#ifdef __GNUC__
#define STATIC_NORETURN(sig) static void sig __attribute__((__noreturn__))
#define EXTERN_NORETURN(sig) extern void sig __attribute__((__noreturn__))
#endif

#ifdef __WATCOMC__
#pragma aux noreturn aborts;
#define STATIC_NORETURN(sig) static void __pragma("noreturn") sig
#define EXTERN_NORETURN(sig) extern void __pragma("noreturn") sig
#endif

STATIC_NORETURN(my_static_example(void));
EXTERN_NORETURN(my_extern_example(void));

static void my_static_example(void) {
    exit(0);
}

void my_extern_example(void) {
    my_static_example();
}

它使用GCC编译,没有任何错误或警告:

代码语言:javascript
复制
$ gcc -Wall -Wextra -pedantic -std=gnu99 -c test.c

但不使用Open Watcom 1.9:

代码语言:javascript
复制
$ wine wcc386 -q -wx test.c
test.c(14): Error! E1009: Expecting ')' but found 'noreturn'
test.c(14): Error! E1026: Invalid declarator
test.c(14): Error! E1009: Expecting ',' but found 'noreturn'
test.c(14): Error! E1026: Invalid declarator
test.c(14): Error! E1009: Expecting ',' but found ')'
test.c(14): Error! E1024: Declared symbol 'my_static_example' is not in parameter list
test.c(15): Error! E1023: Storage class of parameter must be register or unspecified
test.c(15): Error! E1009: Expecting ')' but found 'noreturn'
test.c(15): Error! E1024: Declared symbol '__pragma' is not in parameter list
test.c(15): Error! E1009: Expecting ',' but found 'noreturn'
test.c(15): Error! E1026: Invalid declarator
test.c(15): Error! E1009: Expecting ',' but found ')'
test.c(15): Error! E1024: Declared symbol 'my_extern_example' is not in parameter list
test.c(17): Error! E1023: Storage class of parameter must be register or unspecified
test.c(17): Error! E1024: Declared symbol 'my_static_example' is not in parameter list
test.c(17): Error! E1076: Missing semicolon at end of declaration
test.c(22): Warning! W131: No prototype found for function 'my_static_example'
test.c(14): Warning! W202: Symbol '__pragma' has been defined, but not referenced

仔细阅读后,我认为手册上说,由于某种原因,__pragma只能在C++代码中工作。但我不确定我是否理解正确。对于C语言,有没有不需要在每个函数定义的地方使用#pragma的等价物?我尽量避免使用ifdefs,所以最好在一个中心位置定义一个可移植的NORETURN宏。

EN

回答 1

Stack Overflow用户

发布于 2021-08-28 09:58:04

阅读https://open-watcom.github.io/open-watcom-v2-wikidocs/cguide.html

代码语言:javascript
复制
__declspec( noreturn )
    indicates to the C/C++ compiler that function does not return.
    Example:
    ...

要兼容stdnoreturn.h,不要想出你自己的风格。执行以下操作:

代码语言:javascript
复制
// possibly in some noreturn.h
// maybe even name it stdnoreturn.h
#if __STDC_VERSION__ >= 201110L
#include <stdnoreturn.h>
#elif defined(__WATCOMC__)
#define noreturn __declspec(noreturn)
#else
#error
#endif

#include <stdlib.h>

static noreturn void my_static_example(void) {
    exit(0);
}

noreturn void my_extern_example(void) {
    my_static_example();
}

在使用Version 2.0wcc386 -q -wx test.c上编译正常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57540145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档