2

我正在尝试编译一个包含 Middlc.h 的文件 Args.c。Middlc.h 错误给出以下错误。

enter code here
Middlc.h:23:1: error: pasting "*" and "ArgList" does not give a valid preprocessing token
Middlc.h:24:1: error: pasting "*" and "CandidateList" does not give a valid preprocessing token
Middlc.h:25:1: error: pasting "*" and "Decl" does not give a valid preprocessing token
Middlc.h:26:1: error: pasting "*" and "DeclList" does not give a valid preprocessing token
Middlc.h:27:1: error: pasting "*" and "ExcList" does not give a valid preprocessing token
Middlc.h:28:1: error: pasting "*" and "Field" does not give a valid preprocessing token
Middlc.h:29:1: error: pasting "*" and "FieldList" does not give a valid preprocessing token
Middlc.h:30:1: error: pasting "*" and "Identifier" does not give a valid preprocessing token
Middlc.h:31:1: error: pasting "*" and "IdentifierList" does not give a valid preprocessing token
Middlc.h:32:1: error: pasting "*" and "Interface" does not give a valid preprocessing token
Middlc.h:33:1: error: pasting "*" and "InterfaceList" does not give a valid preprocessing token
Middlc.h:34:1: error: pasting "*" and "Spec" does not give a valid preprocessing token
Middlc.h:35:1: error: pasting "*" and "SymbolTable" does not give a valid preprocessing token
Middlc.h:36:1: error: pasting "*" and "ScopedName" does not give a valid preprocessing token
Middlc.h:37:1: error: pasting "*" and "Type" does not give a valid preprocessing token
Middlc.h:38:1: error: pasting "*" and "TypeList" does not give a valid preprocessing token
Middlc.h:39:1: error: pasting "*" and "FileStack" does not give a valid preprocessing token

即使解决了单个错误,它也会非常有帮助。我可以在此基础上解决其他的。

这是代码:

中间件

#ifndef _Middlc_h_
#define _Middlc_h_

/*
 * Main header file for Middlc.
 */

#define PY(x) printf x 

#define new(x) ((x##_t)malloc(sizeof(struct _##x##_t)))
#define TYPE_DECL(x) typedef struct _##x##_t *##x##_t

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef enum { False, True } bool_t;

#define MAX_STRING_LENGTH 1024

TYPE_DECL(ArgList);
TYPE_DECL(CandidateList);
TYPE_DECL(Decl);
TYPE_DECL(DeclList);
TYPE_DECL(ExcList);
TYPE_DECL(Field);
TYPE_DECL(FieldList);
TYPE_DECL(Identifier);
TYPE_DECL(IdentifierList);
TYPE_DECL(Interface);
TYPE_DECL(InterfaceList);
TYPE_DECL(Spec);
TYPE_DECL(SymbolTable);
TYPE_DECL(ScopedName);
TYPE_DECL(Type);
TYPE_DECL(TypeList);
TYPE_DECL(FileStack);

#include "FileStack.h"
#include "SymbolTable.h"

#include "Modes.h"
#include "Decls.h"
#include "Types.h"
#include "Args.h"
#include "Candidates.h"
#include "Exceptions.h"
#include "Fields.h"
#include "Identifiers.h"
#include "Interfaces.h"
#include "Specs.h"
#include "ScopedNames.h"
#include "Parser.h"

#include "Globals.h"
#include "Errors.h"
#endif /* _Middlc_h_ */
4

2 回答 2

6

线

#define TYPE_DECL(x) typedef struct _##x##_t *##x##_t

应该

#define TYPE_DECL(x) typedef struct _##x##_t * x##_t

(无托克粘贴##)。目前,预处理器试图将星号“粘合”到类似的名称上SymbolTable_t,从而生成不可解析的标识符SymbolTable_t。当您##省略时,编译器会将星号解析为单独的标记,从而修复错误。

于 2012-12-24T13:52:39.653 回答
2

您需要删除 * 和 x 之间的 ## 。那是:

#define TYPE_DECL(x) typedef struct _##x##_t * x##_t

为什么你要做所有这些宏的东西当然是另一回事 - 使用“new”作为宏肯定不好 - 如果你以后想用 C++ 编译器编译你的代码会发生什么(例如,因为一些非常棒的 C++ 代码来了)。

于 2012-12-24T13:52:26.400 回答