Topic : Using a structure in an other file

Forum : ST7/STM8

Original Post
Post Information Post
November 4, 2010 - 9:13am
Guest

Hi everyone,

Description of my problem:

In a "File1.h" file, I defined a structure like that:

typedef struct{
u8 Var1;
u8 Var2;
.
.
.
}sMyStruct;

sMyStruct gMyStruct;

So in "File1.c", I use normaly the definition:

gMyStruct.Var1 = ....;

And for example, I would like to make the same code in "main.c".
So I defined an "extern sMyStruct gMyStruct;" in "main.h"

But I have this error:

main.h syntax error
main.c undefined gMyStruct

I hope my english is not too bad.
Thank you
Regards.

Replies
Post Information Post
+1
0
-1
November 4, 2010 - 2:51pm
Raisonance Support Team

Hi Sylvain,

You should *not* place variable definitions (only declarations) in header files.
This means that "sMyStruct gMyStruct;" shoul dbe in a C file in your project.

Your code works fine if you respect this simple rule, we verified it in our Labs.

If you still have some problems, please send us the listing file produced by the compiler, and a copy of the *exact* error message you receive.

Regards,

+1
0
-1
November 4, 2010 - 8:54pm
Guest

Hi Bruno,

Thank you for this information.
Now it's ok when "sMyStruct gMyStruct;" is in C file.

It's strange because a simple var like "u8 toto;" in H file doesn't make an error when it use in other C file.....

I don't know if it's an ANSI C rule or not.

Regards.

+1
0
-1
November 5, 2010 - 5:59pm
Raisonance Support Team

Hi Sylvain,

If the given .h file is included in several C files, you will have a linker conflict (multiple definition), as each of the C file that includes the .h will try to define the same variable.

BR