If you declare an object 'extern' AND initialize it, it becomes defined. In
this case, 'extern' is usually redundant.
"Jack" wrote:
I don't know why I keep getting unresolved external symbols when I
compile this code
You get this error when you link the program rather than compiling it.
Compilation passes without a problem, right?
// File - prochead.cpp
// ...
extern FILE *inFile, *rcFile;
extern struct ResourceTable *pResourceEntry;
// file - ne.cpp
// ...
extern FILE *inFile = NULL;
extern FILE *rcFile = NULL;
extern struct ResourceTable *pResourceEntry = NULL;
Error 1 error LNK2019: unresolved external symbol "struct ResourceTable *
pResourceEntry" ...
The LNK2019 (unresolved external symbol) error means that linker searched
for definition of given symbol across all modules, but failed to find it
(to resolve it).
Given your code, this is no wonder since you don't define the above
variables anywhere. When you specify `extern' modifier it tells to the
compiler: don't search for definition, it defined elsewhere, let the
linker find it later. That's why the initialization of extern declarations
in "ne.cpp" file dosen't have any effect.
In order to solve it, you need to select one of project files, where
actual definition will be placed. Compiler independent way to do it is to
define a macro:
#if defined(MAIN_UNIT)
# define EXTERN
#else
# define EXTERN extern
#endif // MAIN_UNIT
Then you use `EXTERN' definition everywhere:
// File - prochead.cpp
EXTERN FILE *inFile;
EXTERN FILE *rcFile;
In one (and only) .CPP files define `MAIN_UNIT', so declarations will omit
`extern' modifier:
// file - ne.cpp
#define MAIN_UNIT
EXTERN FILE *inFile;
EXTERN FILE *rcFile;
...
You can save the hustle with a macro by using Microsoft specific
`__declspec( selectany )' modifier everywhere:
__declspec( selectany ) FILE *inFile;
__declspec( selectany ) FILE *rcFile;
Then linker will pick up one of a project files automatically and put
declarations there. Read more info about `__declspec( selectany )' in
MSDN.
HTH
Alex