typedef重定义错误

来源:互联网 发布:淘宝店铺监控软件 编辑:程序博客网 时间:2024/05/29 13:18

记录一个问题。


Item.h文件:

typedef int Item;


Link.h文件:

#include "Item.h"

void LINKinit(int);


Link.c文件:

#include "stdio.h"

#include "Link.h"

#inlcude "Item.h"


void LINKint(int n)

{

    printf("Create a new LINK.\n");

}


进行编译:gcc -o link.o -c Link.c出现如下错误:

In file included from Link.h:1,
                 from Link.c:2:
Item.h:1: 错误:typedef‘Item’重定义
Item.h:1: 附注:‘Item’的上一个声明在此


修改Item.h:

 #ifndef __ITEM__
 #define __ITEM__
 typedef int Item;
 #endif
这样就可以解决typedef重定义的问题。


这个在linux 2.6.32-220.el6.x86_64上运行 gcc4.4.6时出现。但是在F17(3.3.4-5.fc17.i686)上运行gcc4.7.2时没出现这个问题。

在网上找到一篇比较靠谱的解释,http://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=ce3765bf44e49ef0568a1ad4a0b7f807591d6412。

* c-decl.c (diagnose_mismatched_decls): Give error for duplicate
authorjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>  Sun, 23 May 2010 20:47:16 +0000 (20:47 +0000)committerjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>  Sun, 23 May 2010 20:47:16 +0000 (20:47 +0000)
typedefs with different but compatible types.  Allow duplicate
typedefs with the same type except for pedantic non-C1X, but give
warning for variably modified types.

如果在编译时,加上-pedantic,则会有warning信息。


原创粉丝点击