extern 用于 C 中新定义的结构

发布于 2024-12-27 20:01:11 字数 486 浏览 0 评论 0原文

我在 main 中使用了节点结构,但是节点的定义及其操作操作位于名为 NODE/ 的目录中的文件中,

我创建了 NODE/node.h,其中包含:

typedef struct node node;

struct node
{
        int my_reg;
        node *left;
        node *right;
} ;

我创建了 NODE/node.c 并包含在其中node.h 其中有node_insert node_remove;

但是,我在 school_address.c 中使用节点结构,其中还包含 NODE/node.h 和 NODE/node.c

我尝试将

extern struct node

放入school_address.c

但代码没有编译并抱怨 node.h 中的重新定义

有什么想法吗?

I have main in which I used the node struct however defintion of node and it's manipulation operations are localed in a file in the directory called NODE/

I created NODE/node.h which has:

typedef struct node node;

struct node
{
        int my_reg;
        node *left;
        node *right;
} ;

I created NODE/node.c and include in it node.h which has node_insert node_remove;

However I am using the node struction in school_address.c in which I also include NODE/node.h and NODE/node.c

I tried putting

extern struct node

in school_address.c

Yet the code doesn't compile and complains of redefinition in node.h

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话 2025-01-03 20:01:11

extern 用于变量,而不是类型定义。您应该只在需要了解struct node的所有模块中包含标头;它替换了整个标头的内容,内联。

您不应该做的是将一个 C 文件包含在另一个 C 文件中。相反,您应该在标头中声明常用函数的原型。

例如

#include "node.h"

int main()
{
    // whatever
}

,如果您将 node_insert 的原型放入标头中,

typedef struct node node;

struct node
{
        int my_reg;
        node *left;
        node *right;
};

struct node *node_insert(struct node *, int);  // or whatever the prototype is

int main()
{
    // whatever
}

那么在 C 预处理器处理完它之后,就会变成 struct nodenode_insertmain 中可见。

extern is for variables, not type definitions. You should just include the header in all modules that need to know about struct node; that is substituted for the entire header's content, inline.

What you should not do is include a C file in another C file. Instead, you should declare the prototypes of the common functions in a header.

E.g.,

#include "node.h"

int main()
{
    // whatever
}

becomes, if you put the prototype for node_insert in the header,

typedef struct node node;

struct node
{
        int my_reg;
        node *left;
        node *right;
};

struct node *node_insert(struct node *, int);  // or whatever the prototype is

int main()
{
    // whatever
}

after the C preprocessor is done with it, so struct node and node_insert are visible in main.

昇り龍 2025-01-03 20:01:11

您正在寻找包含防护

具体来说,

#ifndef node_h_
#define node_h_ 1

在node.h的开头使用,并

#endif

在其末尾对应。

另外,请勿包含 .c 文件。

You are looking for an Include Guard.

Specifically, use

#ifndef node_h_
#define node_h_ 1

at the beginning of node.h, and the corresponding

#endif

at the end of it.

Also, do not include .c files.

猫烠⑼条掵仅有一顆心 2025-01-03 20:01:11

当您的头文件被多个 C 文件包含时,您必须将它们包含在预处理器指令中以避免重复定义,例如:

#ifndef __NODE_H_INCLUDED__
#define __NODE_H_INCLUDED__

struct node...

#endif

无论如何,在所有头文件中执行此操作是一个很好的做法。

When your header files are included by multiple C files you have to include them in preprocessor directives to avoid double definitions, such as:

#ifndef __NODE_H_INCLUDED__
#define __NODE_H_INCLUDED__

struct node...

#endif

It's good practice to do this anyway in all your header files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文