【问题标题】:Error while compiling macro __COPYRIGHT with gcc使用 gcc 编译宏 __COPYRIGHT 时出错
【发布时间】:2013-02-21 03:23:00
【问题描述】:

这里是简单的 echo.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

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

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

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

但是为什么它不能在 gcc 中编译呢?

【问题讨论】:

    标签: gcc macros


    【解决方案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 &lt;sys/cdefs.h&gt;更改为#include "aeodefs.h"。

    完成了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多