Problem w/ preprocessor
-
So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:
#ifdef WINVER
//wasn't sure about the best one to use
//if you know of something better than WINVER, please let me know
#include "stdafx.h"
#endif... other includes
... implementation of class
However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean
-
So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:
#ifdef WINVER
//wasn't sure about the best one to use
//if you know of something better than WINVER, please let me know
#include "stdafx.h"
#endif... other includes
... implementation of class
However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean
Do not include atdafx.h in you file. Instead (1) goto projects->settings (2) select all configurations (3) select you file from tree view (4) select C/C++ tab (5) from drop down box select precompiled headers (6) check not using precompiled headers. Now when you compile while using MFC you will not gett the error about finding end of file while looking for precompiled header. Trust in the code Luke. Yea right!
-
So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:
#ifdef WINVER
//wasn't sure about the best one to use
//if you know of something better than WINVER, please let me know
#include "stdafx.h"
#endif... other includes
... implementation of class
However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean
Probably your best option is not to include any
#include <stdafx.h>
and, when compiling into an MFC project, change the settings of the.cpp
alone to "not using precompiled headers". Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:
#ifdef WINVER
//wasn't sure about the best one to use
//if you know of something better than WINVER, please let me know
#include "stdafx.h"
#endif... other includes
... implementation of class
However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean
Thanks to both of you... I had not even thought of that. I am still puzzled as to why I am getting this error, though. The #endif is not unexpected -- it matches the #ifdef and there is no other possible text beforehand to provide an unbalanced #if* #endif pair. --Dean
-
So I have a class that does not make use of MFC/Windows code in any way, though I sometimes use it in MFC projects. However, when I do use the class in an MFC project, VS.net complains that there is no '#include "stdafx.h"' line in the .cpp file of the class. Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) Today I tried to remedy this situation by adding an #ifdef at the beginning of the .cpp file as follows:
#ifdef WINVER
//wasn't sure about the best one to use
//if you know of something better than WINVER, please let me know
#include "stdafx.h"
#endif... other includes
... implementation of class
However, if I add this code (at the very beginning of the file) VS.Net chokes with "fatal error C1020: unexpected #endif". Any ideas? --Dean
Dean Goodman wrote: Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) That is so not true. It's about precompiled headers, and have nothing to do with MFC. If you make a new ATL project, in VC, you also get stdafx.h and .cpp I use precompiled headers in all my projects, even console applications because it speeds up the compile time. - Anders Money talks, but all mine ever says is "Goodbye!"
-
Dean Goodman wrote: Likewise, if I add the line to my file and use the class in a non-MFC project, as you would expect, I get a 'cannot find file "stdafx.h"' (since it doesn't exist in the non-MFC project.) That is so not true. It's about precompiled headers, and have nothing to do with MFC. If you make a new ATL project, in VC, you also get stdafx.h and .cpp I use precompiled headers in all my projects, even console applications because it speeds up the compile time. - Anders Money talks, but all mine ever says is "Goodbye!"
What I really meant was that when you create an MFC project (or ATL -- I wouldn't know since I never create this type of project), VS defaults to using a precompiled header. If you create a console application, precompiled headers are not used by default. I didn't mean to say that PCH's are specific to MFC. I just usually don't mess with that stuff -- I usually want to get my work done quickly ;). --Dean
-
What I really meant was that when you create an MFC project (or ATL -- I wouldn't know since I never create this type of project), VS defaults to using a precompiled header. If you create a console application, precompiled headers are not used by default. I didn't mean to say that PCH's are specific to MFC. I just usually don't mess with that stuff -- I usually want to get my work done quickly ;). --Dean
Dean Goodman wrote: I didn't mean to say that PCH's are specific to MFC. Ok, sorry, then I misunderstood you :-O - Anders Money talks, but all mine ever says is "Goodbye!"
-
Thanks to both of you... I had not even thought of that. I am still puzzled as to why I am getting this error, though. The #endif is not unexpected -- it matches the #ifdef and there is no other possible text beforehand to provide an unbalanced #if* #endif pair. --Dean
The way PCHs are implemented in Visual C++, the compiler igoners everything up to the infamous
#include <stdafx.h>
line, which explains the unbalanced#endif
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Dean Goodman wrote: I didn't mean to say that PCH's are specific to MFC. Ok, sorry, then I misunderstood you :-O - Anders Money talks, but all mine ever says is "Goodbye!"
Wouldn't it just be easier to create a .LIB containing the functions and link to it, rather than messing with the source code every time you use it? No hassle, just #include the .h file and set your linker libs line to include the .lib file. Also, one point of maintenance across all projects that use it.