2013-02-21 112 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