2013-02-21 109 views
0

这里是简单echo.c源代码:错误而编译宏__COPYRIGHT用gcc

#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 

3号线和4是__COPYRIGHT宏。 第12行是__RCSID宏。

如果我删除这两个宏,它会编译成功并正确运行。

一些谷歌搜索后,我知道这两个宏是在sys/cdefs.h中定义的,它们是某种评论消息。

但为什么它不会在gcc中编译?

回答

1

那么通过sys/cdefs.h(Ubuntu 11.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"

完成了!

+0

啊哈!这让我疯狂,谢谢。 – saulspatz 2014-10-09 19:34:48