如何声明中的报头是通过在C多个文件中使用的结构? [英] How to declare a structure in a header that is to be used by multiple files in c?

查看:114
本文介绍了如何声明中的报头是通过在C多个文件中使用的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个结构一个由source.c文件:

If I have a source.c file with a struct:

struct a
{ 
    int i;
    struct b
    {
        int j;
    }
};

如何解决这个结构在​​另一个文件中使用(即 func.c )?

我应该创建一个新的头文件,有声明结构,包括在 func.c

Should I create a new header file, declare the struct there and include that header in func.c?

或者我应该在头文件中定义了整个结构,包括在这两种由source.c func.c ?如何能在结构中声明的extern 这两个文件?

Or should I define the whole struct in a header file and include that in both source.c and func.c? How can the struct be declared extern in both files?

我应该的typedef 呢?如果是这样,怎么样?

Should I typedef it? If so, how?

推荐答案

当一个类型是在一个文件中使用(即func.c文件),它必须是可见的。要做到这一点非常糟糕的方式就是复制粘贴在每个源文件需要它。

if this structure is to be used by some other file func.c how to do it?

When a type is used in a file (i.e. func.c file), it must be visible. The very worst way to do it is copy paste it in each source file needed it.

正确的方法是把它在一个头文件,并包含这个头文件需要的时候。

The right way is putting it in an header file, and include this header file whenever needed.

这是我更喜欢的解决方案,因为它使code高度模块化。我想code你的结构为:

This is the solution I like more, because it makes the code highly modular. I would code your struct as:

#ifndef SOME_HEADER_GUARD_WITH_UNIQUE_NAME
#define SOME_HEADER_GUARD_WITH_UNIQUE_NAME

struct a
{ 
    int i;
    struct b
    {
        int j;
    }
};

#endif

我会使用相同的标题这种结构(即是语义的接口的一部分功能)放功能。

I would put functions using this structure in the same header (the function that are "semantically" part of its "interface").

和通常情况下,我能说出结构名后的文件,再次使用该名称来选择标题警卫定义。

And usually, I could name the file after the structure name, and use that name again to choose the header guards defines.

如果您需要声明使用指针结构的函数,你不需要完整的结构定义。一个简单的向前声明如下:

If you need to declare a function using a pointer to the struct, you won't need the full struct definition. A simple forward declaration like:

struct a ;

就足够了,而且减少耦合。

Will be enough, and it decreases coupling.

这是另一种方式,更容易一些,但不模块化:一些code只需要你的结构的工作仍然要包括所有类型

This is another way, easier somewhat, but less modular: Some code needing only your structure to work would still have to include all types.

在C ++中,这可能会导致并发症有趣,但是这是出题目(没有C ++标签),所以我就不细说了。

In C++, this could lead to interesting complication, but this is out of topic (no C++ tag), so I won't elaborate.

我不明白这一点,也许,但格雷格Hewgill已经在他的职位非常好的答案<一个href=\"http://stackoverflow.com/questions/228684/how-to-declare-a-structure-in-a-header-that-is-to-be-used-by-multiple-files-in-c#228691\">http://stackoverflow.com/questions/228684/how-to-declare-a-structure-in-a-header-that-is-to-be-used-by-multiple-files-in-c#228691.

I fail to see the point, perhaps, but Greg Hewgill has a very good answer in his post http://stackoverflow.com/questions/228684/how-to-declare-a-structure-in-a-header-that-is-to-be-used-by-multiple-files-in-c#228691.


  • 如果您使用的是C ++,没有。

  • 如果您使用的是C,你应该。

原因是,C结构管理可以是一个痛苦:你要申报struct关键字到处使用它:

The reason being that C struct managing can be a pain: You have to declare the struct keyword everywhere it is used:

struct MyStruct ; /* Forward declaration */

struct MyStruct
{
   /* etc. */
} ;

void doSomething(struct MyStruct * p) /* parameter */
{
   struct MyStruct a ; /* variable */
   /* etc */
}

虽然一个typedef将使你写没有结构的关键字。

While a typedef will enable you to write it without the struct keyword.

struct MyStructTag ; /* Forward declaration */

typedef struct MyStructTag
{
   /* etc. */
} MyStruct ;

void doSomething(MyStruct * p) /* parameter */
{
   MyStruct a ; /* variable */
   /* etc */
}

重要提示您仍然保持了结构的名称。写作:

It is important you still keep a name for the struct. Writing:

typedef struct
{
   /* etc. */
} MyStruct ;

将只创建一个typedef - ED镜头的名称匿名结构,你将无法前瞻性声明它。因此,保持以下格式:

will just create an anonymous struct with a typedef-ed name, and you won't be able to forward-declare it. So keep to the following format:

typedef struct MyStructTag
{
   /* etc. */
} MyStruct ;

这样,你就可以到处使用MYSTRUCT要避免增加struct关键字,仍然使用MyStructTag当一个typedef将无法正常工作(即向前声明)

Thus, you'll be able to use MyStruct everywhere you want to avoid adding the struct keyword, and still use MyStructTag when a typedef won't work (i.e. forward declaration)

修正错误的假​​设C99左右结构声明,作为理所当然的乔纳森·莱弗勒说。

Corrected wrong assumption about C99 struct declaration, as rightfully remarked by Jonathan Leffler.

这篇关于如何声明中的报头是通过在C多个文件中使用的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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