Dvir Samuel
Dvir Samuel

Reputation: 1168

same struct different files

I've got a little problem. I have 2 files: one.c and two.c they both decler and implement the struct: StackNode the header files are: one.h:

 #ifndef ONE_H
 #define ONE_H

 typedef struct StackNode StackNode;
 #endif

two.h:

 #ifndef TWO_H
 #define TWO_H

 #include "one.h"

 #endif

cpp files: one.c:

 #include <stdio.h>
 #include <malloc.h>
 #include <string.h>
 #include <stdlib.h>
 #include "one.h"

 struct StackNode
 {
   ........
 };

two.c:

 #include <stdio.h>
 #include <malloc.h>
 #include "two.h"
 struct StackNode
 {
   ........
 };

Why this compile and run in linox but in visual stutio it says: two.obj : error LNK2005: "struct StackNode * top" (?top@@3PAUStackNode@@A) already defined in one.obj 1>c:\users\documents\visual studio 2010\Projects\Exercise\Debug\Exercise.exe : fatal error LNK1169: one or more multiply defined symbols found

What can I do so it will work in visual too? Thank you :)

Upvotes: 1

Views: 162

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

The linker does not say that the structure itself is defined twice. It says that object top is defined twice like struct StackNode * top. You have to define it only in one compilation unit.

two.obj : error LNK2005: "struct StackNode * top" (?top@@3PAUStackNode@@A) already defined in one.obj

Upvotes: 1

Related Questions