下面是简单的echo.c .c源代码:
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)echo.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: echo.c,v 1.7 1997/07/20 06:07:03 thorpej Exp $");
#endif
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main __P((int, char *[]));
int
main(argc, argv)
int argc;
char *argv[];
{
/*
*main code with no error at all
*/
}用gcc 4.4.6编译时,报告错误:
echo.c:4: error: expected declaration specifiers or â...â before string constant
echo.c:3: warning: data definition has no type or storage class
echo.c:12: error: expected declaration specifiers or â...â before string constant
echo.c:12: warning: data definition has no type or storage class第三行和第四行是__COPYRIGHT宏。第12行是__RCSID宏。
如果我删除这两个宏,它将成功编译并正确运行。
通过谷歌搜索,我知道这两个宏是在sys/cdefs.h中定义的,它们是某种注释消息。
但是为什么它不能在gcc编译呢?
发布于 2013-02-21 12:04:08
在浏览了sys/cdefs.h (ubuntu11.10)之后,我没有找到__COPYRIGHT或__RCSID的定义。所以我猜这两个宏是在NetBSD sys/cdefs.h中定义的。我将它们添加到一个新的头文件中(我用“aeodefs.h”命名它),如下所示:
#ifndef _AEODEFS_H_
#define _AEODEFS_H_
#include <sys/cdefs.h>
#define __IDSTRING(name,string) \
static const char name[] __attribute__((__unused__)) = string
#ifndef __RCSID
#define __RCSID(s) __IDSTRING(rcsid,s)
#endif
#ifndef __COPYRIGHT
#define __COPYRIGHT(s) __IDSTRING(copyright,s)
#endif
#endif /* !_AEODEFS_H_ */然后将#include <sys/cdefs.h>更改为#include "aeodefs.h"。
就这样办!
https://stackoverflow.com/questions/14994026
复制相似问题