函数原型声明编译失败 [英] function prototype declaration fails compilation

查看:76
本文介绍了函数原型声明编译失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 c 的新手.我正在尝试通读Holub的 C编译器设计.在本书附录A中名为 set.c 的文件中,作者使用了 function prototype 声明,如下所示.

I am new to c. I am trying to read through Holub's Compiler Design In C. In a file called set.c in Appendix A of the book, the author uses a function prototype declaration as shown below.

extern int      _addset     P((SET* , int ));

这对我来说在编译过程中失败.错误在下面列出.我正在使用 gcc(Ubuntu 4.8.4-2ubuntu1〜14.04.1)4.8.4 .

This fails during compilation for me. The error is listed below. I am using gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4.

include/tools/set.h:25:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘P’
 extern void     delset              P((SET* ));

当我如下所示更改声明时,代码将编译.

When I changed the declaration as shown below, the code compiles.

extern int      _addset(SET* , int );

由于我不太熟悉 c ,因此不确定通过更改这样的代码是否做错了什么.谁能告诉我上面的语法是否有效,为什么书中的语法不能编译?Holub确实强调了使用ANSI C的重要性,但是基于我的阅读,GCC符合ANSI(或更恰当地说是ISO)标准.

As I am not too well versed with c, I am not certain if I am doing something wrong by changing the code like this. Could anybody tell me if the above syntax is valid and why doesn't the syntax from the book compile ? Holub does stress on the importance of using ANSI C, but based on what I read GCC is ANSI (or more properly ISO) compliant.

推荐答案

问题是 P 预处理程序宏.C语言中的Compiler Design很旧,因此作者为类似的旧(现在很古老)的编译器提供了有关参数列表的解决方法.这本书描述了它的用途:

The problem is the P preprocessor macro. The Compiler Design in C book is old, so the author has provided a workaround for similarly old (now ancient) compilers regarding argument lists. The book describes what it's for:

第25和28行的P宏处理另一个与ANSI相关的可移植性问题.许多编译器(包括许多UNIX编译器)无法处理函数原型.该宏使用类似的机制到前面讨论的D()宏,以将原型转换为如果未定义ANSI,则使用简单的extern声明.例如,给定以下输入:

The P macro on lines 25 and 28 handles another ANSI-related portability problem. Many compilers (including many UNIX compilers) can't handle function prototypes. This macro uses a mechanism similar to the D () macro discussed earlier to translate prototypes into simple extern declarations if ANSI is not defined. For example, given the following input:

  int dimitri P(( int x, long y ));

如果定义了ANSI,则P(x)宏将求值为其参数,并且以下翻译结果:

if ANSI is defined, the P (x) macro evaluates to its argument and the following translation results:

  int dimitri ( int x, long y );

否则,宏将放弃其参数,并求值为(),因此将创建以下内容:

otherwise, the macro discards its argument and evaluates to () , so the following will be created:

int dimitri();

int dimitri ();

这样的想法是,如果您具有符合ANSI的编译器,则应该在包含定义 P 的头文件之前先 #define ANSI .然后 P 不理会您的参数列表.但是,如果没有兼容ANSI的编译器,则无需定义 ANSI ,并且预处理器会删除您的参数列表. P 看起来像这样:

The idea being that if you have an ANSI-compliant compiler, you're supposed to #define ANSI before including the header file that defines P. Then P leaves your argument list alone. But if you don't have an ANSI-compliant compiler, you don't define ANSI and the preprocessor removes your argument list. P looks like this:

#ifdef ANSI
#define P(x) x
#else
#define P(x) ()
#endif

我建议像您一样完全删除 P .但是,如果您希望能够原样从书中复制并粘贴代码,则也可以 #define ANSI #define P(x)x .

I recommend removing P altogether, like you did. However, you can also #define ANSI or #define P(x) x if you want to be able to copy-and-paste code from the book as-is.

这篇关于函数原型声明编译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆