How to use a specific structure from another source file?

I use Linux as my programming platform and C as a programming language.

My problem is that I define the structure in the main source file (main.c):

struct test_st { int state; int status; }; 

Therefore, I want this structure to be used in another source file (e.g. otherrc.). Is it possible to use this structure in another source file without putting this structure in the header?

+4
source share
6 answers

You can use pointers to it in othersrc.c without including it:

othersrc.c:

 struct foo { struct test_st *p; }; 

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

main.h:

 struct test_st { int state; int status; }; 

main.c:

 #include "main.h" 

othersrc.c:

 #include "main.h" 

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

+11
source

You can define the structure in each source file, then declare the instance variable once as global and once as 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; 

Then the linker will do the magic, and the 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 need to manually change each definition.

A simple solution is to put the definition in the header file and then include it in the entire source file that uses the structure. To access a single instance of the structure in the source files, you can still use the extern method.

 // 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; 
+13
source

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

By prohibiting this, you can treat main.c as a header file and include it in another file, and then compile another file. Or you can declare the same structure in both files and leave a note for yourself to change it in both places.

+4
source

C support separate compilation .

Place the structure declaration in the header file and #include "..." in the source files.

+2
source
 // use a header file. It 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); 
+2
source

The header file / * includes this header file in file1.c and file2.c

 strcut a { }; struct b { }; 

therefore, the header file included the declaration of both structures.

 file 1.c 

strcut a xyz[10]; → struct a defined here

use struct b here in this file

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

file2.c

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

use strcut a defined in file1.c

 use extern struct a xyz[10] 
0
source

Source: https://habr.com/ru/post/1312811/


All Articles