MFC / precompiled header questions

Hello all again... my third question. I am trying to convert my Win32 Calculator app I have written to MFC using the MFC App wizard. Can someone explain precompiled headers for an idiot who doesn't know anything about them? And help me figure out how to get my Calculator.h included in the program without the linker complaining that I'm redefining symbols?

The problem: I have Calculator.h, which defines the Calculator class itself along with some enums, etc. It also includes Number.h, a special class I wrote for a Number superclass with Integer and Double children. Number.h contains all the code for the Number class as well as the class definition.

---Calculator.h---
#if !defined CALCULATOR_H
#define CALCULATOR_H
#include <cmath> & several others
...
#include "Number.h"
class Calculator { /* ... the Calculator class def ... */ };
#endif
---END OF CALCULATOR.H---

---Number.h---
#if !defined NUMBER_H
#define NUMBER_H
class Number { ... };
class Double { ... };
/*some Double member function code */
class Integer { ... };
/*some Integer member function code */
#endif
---END OF NUMBER.H---

---Calculator.cpp---
#include "Calculator.h"
/*Actually defines all the functions of Calculator not defined in Calculator.h*/
...
---END OF CALCULATOR.CPP---


Now the app files created by the MFC Wizard are: MFCCalc.cpp & .h
and MFCCalcDlg.cpp & .h (I'm making a dialog app)

I've tried including Calculator.h in MFCCalcDlg.h right after the "afxwin.h" include. I've tried including it in MFCCalc.h right after the precompiled header and resource includes... Everything compiles okay but the linker complains every time that I'm trying to redefine symbols in the two .obj files MFCCalcDlg and MFCCalc. I actually had this same problem originally when I coded my calculator in Win32 and at first I put all the source for Calculator in the separate source file Calculator.cpp & included Calculator.h from all source files. That's what caused me to include all the code for Calculator in the .h file before and just include Calculator.h from winmain.cpp. But I tried that this time, and it still complained that I was trying to redefine symbols and I don't know why. So I'm lost now.

edit: and now it won't link at all! I just took out all references to Calculator, deleted the headers/sources from the project, and tried to recompile the app just like it was before I tried to integrate Calculator into it and it's reported link errors now.
nafxcwd.lib(thrdcore.obj) : error LNK2019: unresolved external symbol __endthreadex referenced in function "void __stdcall AfxEndThread(unsigned int,int)" (?AfxEndThread@@YGXIH@Z)
nafxcwd.lib(thrdcore.obj) : error LNK2019: unresolved external symbol __beginthreadex referenced in function "public: int __thiscall CWinThread::CreateThread(unsigned long,unsigned int,struct _SECURITY_ATTRIBUTES *)" (?CreateThread@CWinThread@@QAEHKIPAU_SECURITY_ATTRIBUTES@@@Z)
Debug/MFCCalc.exe : fatal error LNK1120: 2 unresolved externals

It compiled and ran just fine as a dialog box before I tried to integrate my calculator source into it.
Last edited on
Ok the second linking error i reported has gone away. I had switched my library to single-threaded debug, and now i'm in multithreaded debug. But back in multithreaded debug, it still won't link when I include Calculator.h into MFCCalcDlg.h
Just get rid of the precompiled headers by removing
#include <stdafx.h>
from all your source files.

Precompiled headers are a shortcut to decrease compile times. The idea is that the compiler already knows what things are in the standard include files, so there is no need to actually #include them and have the compiler spend time parsing and lexing and analyzing etc.

I suspect that when you mix files that use the precompiled headers with those that don't, the compiler generates object code that uses the same symbol names in multiple object files --and when the linker tries to combine them it complains that it can't have two (or more) of the same thing.

Turning off the precompiled headers should fix it. The other way around would be to make sure all your sources #include <stdafx> before anything else and recompile everything.

Hope this helps.
Last edited on
No such luck. I tried removing all the precompiled header references, and turning off precompiled headers in the project file but it didn't work. Still gave me errors about trying to find the precompiled headers.

My original Win32 project had the same problem and I didn't use precompiled headers with that one. I had to include all my source for Calculator into the Calculator.h file and screw the separate .cpp for it. Now it won't work either way I try it.
Seems to me like you need to open your project directory and delete any VC++ configuration files (so all you have left are .cpp and .h files) and then import them into a new project.

If that doesn't help I don't know what else to do... sorry.
Thanks all. I figured it out today. I'm still a bit of a newbie and I found a site that explained it to me. My header files had more than class definitions... they had function definiitions, constants, etc. and were being compiled once with each file that included them, and then the linker was failing. Separated everything better into 4 source files, 4 header files and now it compiles, links, and runs just fine.
Topic archived. No new replies allowed.