I am using VS2010 to develop an app which includes several windows forms that I am trying to set up global variables for, and I am getting a few errors like:
LNK2005: "wchar_t *dsn"...already defined in ....obj
I have a header file (externals.h) with:
#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H
extern long dbg;
extern wchar_t dsn[50];
extern wchar_t u[30];
extern wchar_t p[30];
#endif
and 2 different forms, each with different namespaces, but both including the above header (#include "externals.h").
One of the form .h files defines the values for these externally declared variables like this:
namespace PWValidationTools{
...
wchar_t dsn[50] =_T("MDOTProjectWise");
wchar_t u[30]=_T("api_admin");
wchar_t p[30]=_T("proce55");
long dbg = 1;
public ref class ValidationSetupForm : public System::Windows::Forms::Form {
}
...
}
The other form file only uses these variables, never defines them.
I am getting the above LNK2005 error only for the variables declared as wchar_t, not the "long" one.
Any ideas why I'm getting the link errors only for the wchar_t variables and how I can fix the problem?
It appears you including "form.h" in multiple compilation units.
This is going to result in multiple definitions, since those lines are definitions and that you're including them multple times. Definitions should never go in a header file. Header files should consist of only delcarations.
BTW, the symbols in your externals.h file are going to be in your global namespace, not your PWValidationTools namespace.