C语言 如何使用另一个源文件中定义的结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3041797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:39:49  来源:igfitidea点击:

How to use a defined struct from another source file?

clinux

提问by domlao

I am using Linux as my programming platform and C language as my programming language.

我使用 Linux 作为我的编程平台,使用 C 语言作为我的编程语言。

My problem is, I define a structure in my main source file( main.c):

我的问题是,我在主源文件(main.c)中定义了一个结构:

struct test_st
{
   int state;
   int status;
};

So I want this structure to use in my other source file(e.g. othersrc.). Is it possible to use this structure in another source file without putting this structure in a header?

所以我希望在我的其他源文件(例如 othersrc.)中使用这个结构。是否可以在另一个源文件中使用此结构而不将此结构放在标题中?

回答by lornova

You can define the struct in each source file, then declare the instance variable once as a global, and once as an extern:

您可以在每个源文件中定义结构,然后将实例变量声明一次为全局变量,一次声明为外部变量:

// File1.c
struct test_st
{
   int state;
   int status;
};

struct test_st g_test;

// File2.c
struct test_st
{
   int state;
   int status;
};

extern struct test_st g_test;

The linker will then do the magic, both source file will point to the same variable.

然后链接器会变魔术,两个源文件都将指向同一个变量。

However, duplicating a definition in multiple source files is a bad coding practice, because in case of changes you have to manually change each definition.

但是,在多个源文件中复制定义是一种糟糕的编码习惯,因为在发生更改时,您必须手动更改每个定义。

The easy solution is to put the definition in an header file, and then include it in all the source file that use the structure. To access the same instance of the struct across the source files, you can still use the externmethod.

简单的解决方案是将定义放在一个头文件中,然后将它包含在所有使用该结构的源文件中。要跨源文件访问结构的相同实例,您仍然可以使用该extern方法。

// Definition.h
struct test_st
{
   int state;
   int status;
};

// File1.c
#include "Definition.h"
struct test_st g_test;

// File2.c
#include "Definition.h"  
extern struct test_st g_test;

回答by Matthew Flaschen

You can use pointers to it in othersrc.cwithout including it:

您可以在othersrc.c不包含它的情况下使用指向它的指针:

othersrc.c:

其他src.c:

struct foo
{
  struct test_st *p;
};

but otherwise you need to somehow include the structure definition. A good way is to define it in main.h, and include that in both .c files.

但否则你需要以某种方式包含结构定义。一个好方法是在 main.h 中定义它,并将其包含在两个 .c 文件中。

main.h:

主.h:

struct test_st
{
   int state;
   int status;
};

main.c:

主文件:

#include "main.h"

othersrc.c:

其他src.c:

#include "main.h"

Of course, you can probably find a better name than main.h

当然,你可能会找到比 main.h 更好的名字

回答by drawnonward

Putting it in a header file is the normal, correct way to declare types shared between source files.

将它放在头文件中是声明源文件之间共享类型的正常、正确的方法。

Barring that, you can treat main.c as a header file and include it in the other file, then only compile the other file. Or you can declare the same struct in both files and leave a note to yourself to change it in both places.

除此之外,您可以将 main.c 视为头文件并将其包含在另一个文件中,然后只编译另一个文件。或者您可以在两个文件中声明相同的结构,并给自己留个便条以在两个地方更改它。

回答by Grimper

// use a header file.  It's the right thing to do.  Why not learn correctly?

//in a "defines.h" file:
//----------------------

typedef struct
{
   int state; 
   int status; 
} TEST_ST; 


//in your main.cpp file:
//----------------------

#include "defines.h"

TEST_ST test_st;


    test_st.state = 1;
    test_st.status = 2;




//in your other.ccp file:

#include "defines.h"

extern TEST_ST test_st;

   printf ("Struct == %d, %d\n", test_st.state, test_st.status);

回答by Nikolai Fetissov

C support separate compilation.

C 支持单独编译

Put structure declaration into a headerfile and #include "..."it in the source files.

将结构声明放入文件中,并将#include "..."其放入源文件中。

回答by OS2

It is perfectly reasonable to be inclusive with structs by leaving them in the source file instead. This is encapsulation. However if you're going to redefine struct multiple times in multiple source files then you might as well define the struct once in a header file instead and include that file as necessary.

通过将结构保留在源文件中来包含结构是完全合理的。这就是封装。但是,如果您要在多个源文件中多次重新定义 struct ,那么您最好在头文件中定义一次 struct 并根据需要包含该文件。

回答by Shivraj Singh Shekhawat

Header file /* include this header file in both file1.c and file2.c

头文件 /* 在 file1.c 和 file2.c 中都包含这个头文件

strcut a {

};

struct b {

}; 

so header file included the declaration of both structures .

所以头文件包括两个结构的声明。

file 1.c 

strcut a xyz[10];--> struct a defined here

strcut a xyz[10];--> struct a 在这里定义

to use struct b here in this file

在此文件中使用 struct b

extern struct b abc[20];

/* now can use in this file */

file2.c

文件2.c

strcut b abc[20]; /* defined here */

to use strcut a defined in file1.c

使用在 file1.c 中定义的 strcut a

use extern struct a xyz[10]