Visual C++ Linker error
Posted on 2003-03-23
This should be a pretty easy one for most of you:
I have a Visual C++ .NET project with 4 files in it. 2 *.cpp and 2 *.h files.
//Contians WinMain, message loop, WndProc
// Contains some misc. functions
// Contains a few global variables, and function declarations
// for functions in file1.cpp, plus other #includes for file1.cpp
// Contains function declarations and globals for functions in
So, what *I* expect out of this is that, whichever file gets compiled first will #include "file2.h", and the next #include for "file2.h" will pretty much be ignored because of the #ifndef statement. When I go to comple, though, I end up with the following:
error LNK2005: "[...]" already defined in file1.obj
where [...] represents everything that was declared in file1.h
If I remove file2.cpp from the project, and remove all calls for functions implemented in file2.cpp, the program compiles fine, but I don't understand why it has a problem with this setup. I'm not using pre-compiled headers or anything fancy. I'm surprised I haven't come across this before, since it seems like a really simple thing that I'm missing here. Any ideas?