首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用gcc编译宏__COPYRIGHT出错

用gcc编译宏__COPYRIGHT出错
EN

Stack Overflow用户
提问于 2013-02-21 11:23:00
回答 1查看 414关注 0票数 0

下面是简单的echo.c .c源代码:

代码语言:javascript
复制
#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编译时,报告错误:

代码语言:javascript
复制
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编译呢?

EN

回答 1

Stack Overflow用户

发布于 2013-02-21 12:04:08

在浏览了sys/cdefs.h (ubuntu11.10)之后,我没有找到__COPYRIGHT__RCSID的定义。所以我猜这两个宏是在NetBSD sys/cdefs.h中定义的。我将它们添加到一个新的头文件中(我用“aeodefs.h”命名它),如下所示:

代码语言:javascript
复制
#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"

就这样办!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14994026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档