的typedef重新定义 [英] redefinition of typedef

查看:135
本文介绍了的typedef重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能这样做不正确的,这是多大的,为什么它工作在一个编译器,而不是其他问题。

我有一个大C应用程序,而我试图按照不从其他头文件中包括头文件中的风格。相反,使用前向声明;因此,我想以下几点。

I have a large C application, and I am trying to follow the style of not including header files from within other header files. Instead, using forward declarations; thus I am trying the following.

// in A.h
typedef struct A_ A;
typedef struct B_ B;
struct A_ {
    double a;
    B *b;
};

// in B.h
typedef struct B_ B;
struct B_ {
    int c;
};

// in C.h
typedef struct A_ A;
typedef struct B_ B;
void function_do_something(A*, B*);

// in C.c
#include "A.h"
#include "B.h"
#include "C.h"
void function_do_something(A* a, B* b) {
    ...
}

此范例编译并在Ubuntu 11.10中运行的gcc - 但它给出的OpenSUSE gcc编译器误差修改了说的typedef重新定义

This paradigm compiles and runs in Ubuntu 11.10 gcc -- but it gives compiler erros in OpenSUSE gcc that say "redefinition of typedef".

我一直在做我的Ubunutu发展等还没有意识到,这种模式可能不正确。难道只是,这是完全错误的和Ubuntu的GCC就是太好了?

I have been doing my development in Ubunutu and so hadn't realised that this paradigm might be incorrect. Is it just that this is plain wrong and Ubuntu's gcc is being too nice?

推荐答案

我感到意外,因为我相当肯定,重新声明在同一范围内的同一类型定义在C法律++,但显然这是不合法的用C

I was surprised by this because I'm fairly sure that redeclaring the same typedef in the same scope is legal in C++, but apparently it is not legal in C.

首先,typedef名称没有连接的:

First, typedef names have no linkage:

ISO / IEC 9899:1999 + TC3 6.2.6 / 6:

ISO/IEC 9899:1999 + TC3 6.2.6/6:

以下标识符没有连接的:声明的标识符比任何其他
  一个对象或一个函数[...]

The following identifiers have no linkage: an identifier declared to be anything other than an object or a function [...]

和6.7 ​​/ 3

and 6.7/3:

如果一个标识符没有连锁,应当有所述标识符的不超过一个声明
  (在一个声明符或类型说明符)具有相同的范围,并在相同的名称空间,除了
  为代码,如6.7.2.3规定。

If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3.

所以,你需要确保每个typedef声明在每个转换单元文件范围内只出现一次。

So you need to ensure that each typedef declaration appears only once at file scope in each translation unit.

这篇关于的typedef重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆