【问题标题】:How to use a defined struct from another source file?如何使用另一个源文件中定义的结构?
【发布时间】:2010-06-15 00:14:48
【问题描述】:

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

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

struct test_st
{
   int state;
   int status;
};

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

【问题讨论】:

  • 当然可以,只要在您尝试并包含任何尝试使用它的文件之前定义它。
  • 我不明白你为什么不想包含标题。

标签: c linux


【解决方案1】:

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

// 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;

然后链接器将发挥作用,两个源文件将指向同一个变量。

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

简单的解决方案是将定义放在头文件中,然后将其包含在所有使用该结构的源文件中。要跨源文件访问结构的相同实例,您仍然可以使用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;

【讨论】:

  • @kuhaku:您应该在头文件中定义结构。复制定义是一种非常糟糕的编程习惯,因为您必须手动更改每个定义以防发生更改。
【解决方案2】:

您可以在othersrc.c 中使用指向它的指针而不包括它:

othersrc.c:

struct foo
{
  struct test_st *p;
};

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

main.h:

struct test_st
{
   int state;
   int status;
};

main.c:

#include "main.h"

othersrc.c:

#include "main.h"

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

【讨论】:

  • 就像在 othersrc.c 中一样,我必须定义 struct test_st *lcst; ?
  • 不需要前向声明,只需声明指针即可。所以你可以写一个函数void foo(test_st *lcst); 但是你可以看到结构中的任何字段,所以任何需要处理它里面的数据的东西都需要放在 main.c 中,或者你可以把结构定义放在 header
【解决方案3】:
// 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);

【讨论】:

  • 真的不需要 typedef,有些人不喜欢它们
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    C 支持separate compilation.

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

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        struct a {
        
        };
        
        struct b {
        
        }; 
        

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

        file 1.c 
        

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

        在此文件中使用 struct b

        extern struct b abc[20];
        
        /* now can use in this file */
        

        file2.c

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

        使用file1.c中定义的struct a

        use extern struct a xyz[10]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-02
          • 2015-06-03
          • 2013-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-18
          • 1970-01-01
          相关资源
          最近更新 更多