link error.
-
i have a
CSocket SomeSocket;
variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as
#include "one.h"
I used this SomeSocket in another two.h files where i have included the file
#include "one.h"
now i get a linker error stating
two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
already defined in one.objwhat was the mistake. -- modified at 6:54 Friday 30th November, 2007
Adi Vishayam
-
i have a
CSocket SomeSocket;
variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as
#include "one.h"
I used this SomeSocket in another two.h files where i have included the file
#include "one.h"
now i get a linker error stating
two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
already defined in one.objwhat was the mistake. -- modified at 6:54 Friday 30th November, 2007
Adi Vishayam
You need to put include guards in your header file:
#ifndef YOURFILE #define YOURFILE // Code here #endif
(replace YOURFILE by something unique, like the file name). You could also use#pragma once
for microsoft compiler.
Cédric Moonen Software developer
Charting control [v1.2] -
You need to put include guards in your header file:
#ifndef YOURFILE #define YOURFILE // Code here #endif
(replace YOURFILE by something unique, like the file name). You could also use#pragma once
for microsoft compiler.
Cédric Moonen Software developer
Charting control [v1.2]Cedric Moonen wrote:
You could also use #pragma once for microsoft compiler
#pragama once is already added. problem remains the same.
Adi Vishayam
-
i have a
CSocket SomeSocket;
variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as
#include "one.h"
I used this SomeSocket in another two.h files where i have included the file
#include "one.h"
now i get a linker error stating
two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
already defined in one.objwhat was the mistake. -- modified at 6:54 Friday 30th November, 2007
Adi Vishayam
you have to declare it
extern
inside your include file (i.e."one.h"
)extern CSocket SomeSocket;
and then define once inside a single source file (e.g. in your
"one.cpp"
).CSocket SomeSocket;
The mistake is in your include file: as it stands
CSocket SomeSocket
is defined in multiple source files. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
i have a
CSocket SomeSocket;
variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as
#include "one.h"
I used this SomeSocket in another two.h files where i have included the file
#include "one.h"
now i get a linker error stating
two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
already defined in one.objwhat was the mistake. -- modified at 6:54 Friday 30th November, 2007
Adi Vishayam
CPallini has given you the correct answer. But I see this mistake a lot with novice programmers. The linker is complaining because (as it says) you have two instances of SomeSocket. Before the compiler attacks you "one.c" file, a preprocessor attacks it, and processes the includes. An easy exercise for you: remove the #include from one.c, and cut and paste the whole of one.h into the top of one.c. Do this for two.c. Now have a lightbulb moment as you realise what the linker is telling you. Iain.