Declare in form1.h causes error
-
Hello, I was experiencing a strange error that I tracked down to be a declaration in Form1.h I attemped to reproduce the problem in a brand new project and to my surprise: the error appeared again. In the new "windows forms" project test1.cpp:
#include "stdafx.h" #include "form1.h"
Added to form1.hint test;
error: test.obj : error LNK2005: "int test" (?test@@$$Q3HA) already defined in IncludeTest.obj D:\VC\IncludeTest\Debug\IncludeTest.exe : fatal error LNK1169: one or more multiply defined symbols found. I have triedint test
in stdafx.h. This does compile though stdafx.h is also included in both the main and test.cpp. Project available here (44k, created with VC++ 2005) I thought this was according to c++ language rules, but apparently I am doing something wrong? Best regards, Jan Timmerman -
Hello, I was experiencing a strange error that I tracked down to be a declaration in Form1.h I attemped to reproduce the problem in a brand new project and to my surprise: the error appeared again. In the new "windows forms" project test1.cpp:
#include "stdafx.h" #include "form1.h"
Added to form1.hint test;
error: test.obj : error LNK2005: "int test" (?test@@$$Q3HA) already defined in IncludeTest.obj D:\VC\IncludeTest\Debug\IncludeTest.exe : fatal error LNK1169: one or more multiply defined symbols found. I have triedint test
in stdafx.h. This does compile though stdafx.h is also included in both the main and test.cpp. Project available here (44k, created with VC++ 2005) I thought this was according to c++ language rules, but apparently I am doing something wrong? Best regards, Jan TimmermanIf you have not already, try adding guards to your header-file. I am not sure about your background, but each header-file should have a guard: test1.h: #ifndef _TEST1_H_ #define _TEST1_H_ ..... your header information ..... #endif test2.h: #ifndef _TEST2_H_ #define _TEST2_H_ ..... your header information ..... #endif etc.
-
Hello, I was experiencing a strange error that I tracked down to be a declaration in Form1.h I attemped to reproduce the problem in a brand new project and to my surprise: the error appeared again. In the new "windows forms" project test1.cpp:
#include "stdafx.h" #include "form1.h"
Added to form1.hint test;
error: test.obj : error LNK2005: "int test" (?test@@$$Q3HA) already defined in IncludeTest.obj D:\VC\IncludeTest\Debug\IncludeTest.exe : fatal error LNK1169: one or more multiply defined symbols found. I have triedint test
in stdafx.h. This does compile though stdafx.h is also included in both the main and test.cpp. Project available here (44k, created with VC++ 2005) I thought this was according to c++ language rules, but apparently I am doing something wrong? Best regards, Jan TimmermanThe problem is that memory for that variable is being allocated in every source file that includes that header file - the linker is unable to determine which one to keep and which ones to throw away. All it knows is that the two memory locations have the same name - it doesn't know that they are the same variable. The solution is to mark the variable as
extern
in the header file, and declare it as normal in exactly one of the source files.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
The problem is that memory for that variable is being allocated in every source file that includes that header file - the linker is unable to determine which one to keep and which ones to throw away. All it knows is that the two memory locations have the same name - it doesn't know that they are the same variable. The solution is to mark the variable as
extern
in the header file, and declare it as normal in exactly one of the source files.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Perhaps that might work. This does not explain though why it is allowed to say
int test;
in stdafx.h (which is included also in a lot of places) Also I thoughtint test;
is a declaration here. It would be illegal to writeint test=3;
. I will give your suggestions a closer look, thanks for your quick replies! -- modified at 7:47 Monday 6th March, 2006 Well, it works, thanks a lot! -
Perhaps that might work. This does not explain though why it is allowed to say
int test;
in stdafx.h (which is included also in a lot of places) Also I thoughtint test;
is a declaration here. It would be illegal to writeint test=3;
. I will give your suggestions a closer look, thanks for your quick replies! -- modified at 7:47 Monday 6th March, 2006 Well, it works, thanks a lot!jantimmerman wrote:
This does not explain though why it is allowed to say int test; in stdafx.h (which is included also in a lot of places)
It's because you're using precompiled headers. VC++ only compiles the header once, and just references it in all the other files that include it. If you turn off precompiled headers, you'll have the same problem there.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"