must I include stdafx.h?

Hello,
    I am trying to use a couple files in two different systems, one of which
being an MFC project. What is the best way to write a fairly portable cpp
file? When I bring a certain cpp file over to my MFC project, it initially
will not compile because it doesn't have #include "stdafx.h" at the top. I
thought about adding something like this to the top of the file:

#if defined(_WINDOWS)
#include "stdafx.h"
#endif

so that MFC would be happy, and my other system would not complain about not
being able to find stdafx.h. However, this does not compile either: "fatal
error C1020: unexpected #endif"

I also thought about not using precompiled headers, but I don't really know
what the best solution is. I really only have a couple files that I'd like
to be able to share across my two systems, so I'd like to avoid tearing up
my MFC project just to make this work.

thanks,
Mitch


0
mitch199 (14)
2/17/2004 12:32:53 AM
vc.mfc 33608 articles. 0 followers. Follow

7 Replies
1483 Views

Similar Articles

[PageSpeed] 42

Umm...   Don't you mean

#ifdef    _WINDOWS
....
#endif

Also, you need to include stdafx.h in any file in a project for which you've
selected the option to use pre-compiled headers (which is the default).

I've done several console apps (mostly where I have to maintain
compatibility with other OS) which do not use pre-compiled headers and they
work just fine

    Norm

"mitch" <mitch@nospam.com> wrote in message
news:VidYb.8923$PY.1496@newssvr26.news.prodigy.com...
> Hello,
>     I am trying to use a couple files in two different systems, one of
which
> being an MFC project. What is the best way to write a fairly portable cpp
> file? When I bring a certain cpp file over to my MFC project, it initially
> will not compile because it doesn't have #include "stdafx.h" at the top. I
> thought about adding something like this to the top of the file:
>
> #if defined(_WINDOWS)
> #include "stdafx.h"
> #endif
>
> so that MFC would be happy, and my other system would not complain about
not
> being able to find stdafx.h. However, this does not compile either: "fatal
> error C1020: unexpected #endif"
>
> I also thought about not using precompiled headers, but I don't really
know
> what the best solution is. I really only have a couple files that I'd like
> to be able to share across my two systems, so I'd like to avoid tearing up
> my MFC project just to make this work.
>
> thanks,
> Mitch
>
>

0
ndrez (136)
2/17/2004 1:02:07 AM
mitch wrote:

>Hello,
>    I am trying to use a couple files in two different systems, one of which
>being an MFC project. What is the best way to write a fairly portable cpp
>file? When I bring a certain cpp file over to my MFC project, it initially
>will not compile because it doesn't have #include "stdafx.h" at the top. I
>thought about adding something like this to the top of the file:
>
>#if defined(_WINDOWS)
>#include "stdafx.h"
>#endif
>
>so that MFC would be happy, and my other system would not complain about not
>being able to find stdafx.h. However, this does not compile either: "fatal
>error C1020: unexpected #endif"
>
>I also thought about not using precompiled headers, but I don't really know
>what the best solution is. I really only have a couple files that I'd like
>to be able to share across my two systems, so I'd like to avoid tearing up
>my MFC project just to make this work.

Here are some options for you:

1. In your MFC project, disable precompiled headers on the shared files that
don't #include "stdafx.h".

2. Write #include "stdafx.h" at the top of the shared files and create an
empty header "stdafx.h" in the non-MFC project.

3. In your MFC project, create files that do this:

 // This file is named (say) MFC_shared1.cpp

 #include "stdafx.h"

 #include "shared1.cpp"

4. Instead of sharing files, compile them into a library and share
libraries.

-- 
Doug Harrison
Microsoft MVP - Visual C++
0
dsh (2498)
2/17/2004 1:17:16 AM
Norm,

Thanks for your response. Can you tell me what the difference is between:

#ifdef _WINDOWS
//and
#if defined(_WINDOWS)

thanks,
mitch


"Norm Dresner" <ndrez@att.net> wrote in message
news:jKdYb.9193$aH3.306867@bgtnsc04-news.ops.worldnet.att.net...
> Umm...   Don't you mean
>
> #ifdef    _WINDOWS
> ...
> #endif
>
> Also, you need to include stdafx.h in any file in a project for which
you've
> selected the option to use pre-compiled headers (which is the default).
>
> I've done several console apps (mostly where I have to maintain
> compatibility with other OS) which do not use pre-compiled headers and
they
> work just fine
>
>     Norm
>
> "mitch" <mitch@nospam.com> wrote in message
> news:VidYb.8923$PY.1496@newssvr26.news.prodigy.com...
> > Hello,
> >     I am trying to use a couple files in two different systems, one of
> which
> > being an MFC project. What is the best way to write a fairly portable
cpp
> > file? When I bring a certain cpp file over to my MFC project, it
initially
> > will not compile because it doesn't have #include "stdafx.h" at the top.
I
> > thought about adding something like this to the top of the file:
> >
> > #if defined(_WINDOWS)
> > #include "stdafx.h"
> > #endif
> >
> > so that MFC would be happy, and my other system would not complain about
> not
> > being able to find stdafx.h. However, this does not compile either:
"fatal
> > error C1020: unexpected #endif"
> >
> > I also thought about not using precompiled headers, but I don't really
> know
> > what the best solution is. I really only have a couple files that I'd
like
> > to be able to share across my two systems, so I'd like to avoid tearing
up
> > my MFC project just to make this work.
> >
> > thanks,
> > Mitch
> >
> >
>


0
mitch199 (14)
2/17/2004 1:27:47 AM
None.  They are functionally equivalent.

--
Frank

"mitch" <mitch@nospam.com> wrote in message
news:n6eYb.8937$PY.7348@newssvr26.news.prodigy.com...
> Norm,
>
> Thanks for your response. Can you tell me what the difference is between:
>
> #ifdef _WINDOWS
> //and
> #if defined(_WINDOWS)
>
> thanks,
> mitch


0
2/17/2004 2:22:24 AM
To answer your subject: Yes. No. Maybe.

To write portable functions, where the C or C++ source code is shared, you need to go into
project | settings, C/C++, Precompiled Headers, select the file you are concerned about,
and disable the use of precompiled headers. Then you don't need the conditional you have
shown. It is useful to include a comment like

// If you get a compilation error failure to find stdafx.h, please disable precompiled
// headers for this file.

in the file so if anyone uses it in a C/C++ project  they will also know they must remove
the precompiled header option. 

The reason you get the error compiling is that the compiler, literally, ignores everything
up to the #include of stdafx.h, so the #if defined is essentailly a comment. It will not
be seen by the compiler. 

So there is no reason not to use precompiled headers; you can simply selectively disable
their use for the common source code files.
				joe
 
On Tue, 17 Feb 2004 00:32:53 GMT, "mitch" <mitch@nospam.com> wrote:

>Hello,
>    I am trying to use a couple files in two different systems, one of which
>being an MFC project. What is the best way to write a fairly portable cpp
>file? When I bring a certain cpp file over to my MFC project, it initially
>will not compile because it doesn't have #include "stdafx.h" at the top. I
>thought about adding something like this to the top of the file:
>
>#if defined(_WINDOWS)
>#include "stdafx.h"
>#endif
>
>so that MFC would be happy, and my other system would not complain about not
>being able to find stdafx.h. However, this does not compile either: "fatal
>error C1020: unexpected #endif"
>
>I also thought about not using precompiled headers, but I don't really know
>what the best solution is. I really only have a couple files that I'd like
>to be able to share across my two systems, so I'd like to avoid tearing up
>my MFC project just to make this work.
>
>thanks,
>Mitch
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
2/17/2004 5:15:24 PM
"mitch" <mitch@nospam.com> skrev i meddelandet
news:VidYb.8923$PY.1496@newssvr26.news.prodigy.com...
> Hello,
>     I am trying to use a couple files in two different systems, one of
which
> being an MFC project. What is the best way to write a fairly portable cpp
> file? When I bring a certain cpp file over to my MFC project, it initially
> will not compile because it doesn't have #include "stdafx.h" at the top. I
> thought about adding something like this to the top of the file:
>
> #if defined(_WINDOWS)
> #include "stdafx.h"
> #endif
>
> so that MFC would be happy, and my other system would not complain about
not
> being able to find stdafx.h. However, this does not compile either: "fatal
> error C1020: unexpected #endif"

This doesn't work because everything up to and including the line
#include "stdafx.h"
is (of course :-) taken from the precompiled header. Any code you add before
this include will be ignored.

>
> I also thought about not using precompiled headers, but I don't really
know
> what the best solution is. I really only have a couple files that I'd like
> to be able to share across my two systems, so I'd like to avoid tearing up
> my MFC project just to make this work.

In a Visual Studio project you can disable use of precompiled headers on a
file by file basis. Just right click in the file list (Solution Explorer),
and choose Properties. Here you can override most project settings for each
individual file.


Bo Persson

>
> thanks,
> Mitch
>
>


0
bop (114)
2/17/2004 5:15:59 PM
"mitch" <mitch@nospam.com> wrote in message
news:VidYb.8923$PY.1496@newssvr26.news.prodigy.com...
> Hello,
>     I am trying to use a couple files in two different systems, one of
which
> being an MFC project. What is the best way to write a fairly portable cpp
> file? When I bring a certain cpp file over to my MFC project, it initially
> will not compile because it doesn't have #include "stdafx.h" at the top. I
> thought about adding something like this to the top of the file:
>
> #if defined(_WINDOWS)
> #include "stdafx.h"
> #endif
>
> so that MFC would be happy, and my other system would not complain about
not
> being able to find stdafx.h. However, this does not compile either: "fatal
> error C1020: unexpected #endif"
>
> I also thought about not using precompiled headers, but I don't really
know
> what the best solution is. I really only have a couple files that I'd like
> to be able to share across my two systems, so I'd like to avoid tearing up
> my MFC project just to make this work.
>
> thanks,
> Mitch
>
>
Mitch,
    You can disable PCH (Precompiled Headers) for this source file.  Select
the file in the workspace/solution and then go to your project properties.
Go to the "Precompiled Headers" category under the C++ tab.  Select not
using PCH.  For some reason MFC will ignore all text above #include
"stdafx.h".  This is why you got the unexpected #endif, it never saw the
#if!  Good luck!


0
trevor8021 (184)
2/17/2004 6:26:57 PM
Reply:

Similar Artilces:

Boss wants to secretly intercept all outgoing mail for ONE user. h
Dear friends My boss wants to secretly/transparently intercept/divert all outgoing email for one particular user in our system so that the mail can be read by him before it really gets delivered. How can I implement this? Would Exchange 2007 support this tweak? Don't know what version of Exchange you are using, however: I don't know about delaying it's delivery but you can have Exchange 2003 send a copy of all that person's email to your boss. You would go into Active Directory Users and Computers and open that user's AD profile. Go to the "Exchange General&...

Is any tech support included with purchase of Visio Pro 2003?
My co-workers are insisting that some support is included when one purchases products from Microsoft (30 days, maybe?). I can't find anything about how to access it (if, indeed, that's true). Is there any free support available for users, and if so, who do I call/e-mail, or is it all on a $35/issue basis right from the start? Thanks for any info... p.s. I've RTFMed until I'm blue in the face - the answers to my questions (2) are not there. Product support varies by country, so you will need to go to http://support.microsoft.com and then pick the product and country t...

Field1 or Field2 must be null
Is it possible to set a constraint on a table across two fields such that one of the pair must have a value and the other be null? Dan Neely wrote: >Is it possible to set a constraint on a table across two fields such >that one of the pair must have a value and the other be null? You can use the table's Validation Rule: [f1] Is Not Null Or [f2] Is Not Null -- Marsh MVP [MS Access] On Tue, 12 Jun 2007 10:21:03 -0700, Dan Neely <dan.neely@gmail.com> wrote: >Is it possible to set a constraint on a table across two fields such >that one of the pair must have a value...

Cannot Copy a PO with a Project Included
I tried to copy a purchase order that included a project and received the following message "You are not allowed to select a purchase order that contains project accounting records." Why is this? Is there a way around this? Thanks, Jocelyn ...

Outlook abililty to include items in an email.
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Email Client: pop Outlook is able to include previously received or sent email as &quot;items&quot; in a new message (this eliminates the need to forward numerous emails). I've been trying to find this option in Entourage, but to no avail. <br><br>Any ideas? <br><br>Thank you, <br><br>Drew Hi Drew, in Entourage, you can select several messages (hold down the Cmd key for non-contiguous selection) and then click on Message>Forward as Attachment. Alternatively, you can co...

I am getting an message saying"you must be an Adminstrator to...."
I am getting an message saying"you must be an Adminstrator to install Office 2007". Can someone please let me know how do I resolve this issue. It means you must logg on to win using an account that has full writes What version of win? "D" <D@discussions.microsoft.com> wrote in message news:653CE8A1-317C-4FD5-9599-9AE51F3892C4@microsoft.com... >I am getting an message saying"you must be an Adminstrator to install >Office > 2007". Can someone please let me know how do I resolve this issue. Log on to the computer as the administr...

negative [h]:mm:ss does not show on the x axis of a chart
If you have minus hh:mm:ss time on a chart or pivot chart the x axis does not display any values down it's axis. and when you hove over the bar, value is empty. In Excell 2003 it work fine in a normal chart, but not pivot chart. In Excel 2007 neither work. Also when you select the 1904 date system, the times fly through the roof. time is displayed as 1:00:00 on the worksheet, but within the pivottable / chart they are shown as 35089:00:00, formula in the cell = 3600 / 86400. fields / cells all are formatted as [h]:mm:ss ---------------- This post is a suggestion for Microsoft, an...

Must display as: send on behalf of XXXXXXXX
Hi I hav exchange 2003 and xp pro clients. The PA has the right to send on behalf of the GM but in the From: field is showing that the mail comes directly from her which they don't want. How can i change this so that it display : On behalf of the GM(the name of the name) thanks On Thu, 18 May 2006 22:56:01 -0700, Craig from Huguenot <CraigfromHuguenot@discussions.microsoft.com> wrote: >Hi > >I hav exchange 2003 and xp pro clients. The PA has the right to send on >behalf of the GM but in the From: field is showing that the mail comes >directly from her which th...

OK, I must be retarded or something...
OK, I must be retarded or something. I am having the hardest time setting up POP3 mail for external users. When I do, eithe it halts all mail with no immediate return messages or it only allows delivery from an internal user to an internal user. I obviously do not want to have the relay open. That is understood. Here are the latest sub-genius steps I have done with no avail. Exchange 2003 over 2003 native\native AD domain All done under properties of <Server>\SMTP: 1. Accesss tab, Authentication button. Anonymous unchecked, Basic Authentication checked, and the default domain is listed...

Can't find a switch to force ".jpg" attachments to be included in the body of an incoming email
I'm in the process of switching to Outlook from Netscape Messenger, and was used to having attached ".jpg" files expand within the body of the email. Can't seem to find a switch to allow same with Outlook. Thanks for any help. Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =---- Never mind. Guess I need to browse the group list before posting, as there are many instances of this posti...

There must be a way...
Hey All: I've got a quick question that's been bugging me for a while. If I'v got data arranged by columns, but I've written a formula which I wan to drag right (i.e. rearrange the calculated data into rows), then ho can I write a formula so it will just copy over? The problem is that normally when you copy a formula to the right i will increment the column letter by one each time while keep ing th row number referenced in the formula the same. Instead, when I copy i to the right, I want the column letter to remain the same and the ro number referenced in the formula to ...

must I include stdafx.h?
Hello, I am trying to use a couple files in two different systems, one of which being an MFC project. What is the best way to write a fairly portable cpp file? When I bring a certain cpp file over to my MFC project, it initially will not compile because it doesn't have #include "stdafx.h" at the top. I thought about adding something like this to the top of the file: #if defined(_WINDOWS) #include "stdafx.h" #endif so that MFC would be happy, and my other system would not complain about not being able to find stdafx.h. However, this does not compile either: "f...

Setting Sale Price for must enter price items
we are having a store wide sale. There are items that you must enter price at pos. When I set a formula (price*.75) these come up as $0.00 even after entering the price. Is there any way around this??? ...

FAO Mike H
Hello Mike, Apologies for the delay in responding to your answer you gave below Pat, with the amount owed in A1 try this =SUMPRODUCT(--(A1>{0;100;500;2000;10000}), (A1-{0;100;500;2000;10000}), {0.125;-0.085;-0.015;-0.015;-0.0075}) -- Mike If you remember I asked how I could calculate different Levy's. Could I ask a final question Mike in relation to the % used in your reply. Your use of decimal % confused me. Are the following figures in your formula percentages? {0.125;-0.085;-0.015;-0.015;-0.0075}). If so, They don't appear to be % in decimal for...

what must I do?
Hello all: I create a single document and the class view is derived from CListView using AppWizard,look: BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs) { cs.style|=LVS_REPORT; } void CTestView::OnInitialUpdate() { ListView_SetExtendedListViewStyle(this->m_hWnd,LVS_EX_GRIDLINES); TCHAR rgsz[3][10]={"One","Two","Three"}; LV_COLUMN lvcolumn; for(int i=0;i<3;i++) { lvcolumn.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH|LVCF_ORDER; lvcolumn.fmt=LVCFMT_LEFT; lvcolumn.pszText=rgsz[i]; lvcolumn.iSubItem=i; lvcolumn.iOrder=i; lvcolumn.cx=60; GetListCtrl(...

I must be really goofy, but...
I need a formula to help me figure out a percent if cells. ie.. if A = $125.00 and I need to find out what 25% of that num,ber is.. how do I do that? -- jojo1964 ------------------------------------------------------------------------ jojo1964's Profile: http://www.officehelp.in/member.php?userid=8289 View this thread: http://www.officehelp.in/showthread.php?t=1826079 http://www.officehelp.in =A1*25% or A1*0.25 -- Regards, Peo Sjoblom "jojo1964" <jojo1964.3e4bfs@DoNotSpam.com> wrote in message news:jojo1964.3e4bfs@DoNotSpam.com... > > I need a ...

Is it possible for a search to include the archive?
How can I include the Outlook 2002 archive in a search? Beemer If you open the Archive file in your Folder List using File | Open, then use Advanced Find, you can choose the Archive file as well as your default file for the search. -- Jocelyn Fiorello MVP - Outlook *** Messages sent to my e-mail address will NOT be answered -- please reply only to the newsgroup to preserve the message thread. *** In news:MPG.1a858b19fb06231c9896a7@news.microsoft.com, Beemer wrote: > How can I include the Outlook 2002 archive in a search? > > Beemer In article <uutW6uG6DHA.2136@TK2MSFTNGP...

550 You must SMTP Authenticate before sending
I posted this recently and no one has yet recognized the error message. If you've received it and resolved the issue it represents, I hope you will share: It's occasional, particularly with replies; I click send and th following message instantly appears in my InBox: * 550 You must SMTP Authenticate before sending to <email address> I'm using Microsoft Outlook 2002 SP3 My IPS is Roadrunner. I have Norton Internet Security 2005 (NIS). I've removed NIS; no change. I've changed ISPs; no change. In Outlook, I've experimented with the "Log on using Secure P...

I am entering my mobile contacts in xl I want to include zero
in the phone number cell and I cant cause by default when I press enter zero is omitted Muse Pre-format the cells as text (Format>Cells...>Text) or prefix with a single apostrophe ('), this will not show but will retain the leading zero -- HTH Nick Hodge Microsoft MVP - Excel Southampton, England nick_hodgeTAKETHISOUT@zen.co.uk.ANDTHIS "muse calliope" <muse calliope@discussions.microsoft.com> wrote in message news:E7BB679F-FE53-43F4-9CCC-0A0D0DB0293D@microsoft.com... > in the phone number cell and I cant cause by default when I press enter > zero > i...

Choosing File Details to Include
When syncing my device through WMP using WMA files all the file details (such as artist name, album name) were transfered with the file. However, when I tried to sync MP3 files the only info that transferred was the file (song) name - it also had included the the track# as part of the song name. Last night I THINK I came across a window where I could check off which details to include and also had the option to remove the track # from the song name. Today I cannot find that screen anywhere. Where would I have seen such a screen and how do I find it again? Thanks -- ...

How can I easily update formulas that include new rows?
I have a worksheet that I use to update league baseball game stats on a daily basis. Each day as many as 13 new rows are added to the bottom. I then have to go to the top of 10 separate columns one at a time to update SUM formulas so that they will include the new rows. For example, Col C may have a formula at the top of the col that reads: =SUM(C4:C300). After the addition of say 9 rows, I have to manually change the formula to read: =SUM(C4:C309). Is there a way to type that 309 in a blank cell just once and have all the SUM formulas refer to it? TIA for your help. OldKenGoat Hi ...

Custom validation rule
Hi, I have been using this formula to round time values to the nearest 15 minute increment: =ROUND(E10*96,0)/96 This works fine for cells where the value is auto-calculated. However, I have one cell where a value has to be manually entered are strictly 15 minute increments? What I thought I might do is create a Data Validation rule that checks to see if the value * 96 is an integer. If it isn't then it fails. How would I do this? I know it can be done, I am just having a bit of a block. TIA, Jarryd =INT(F13*96)/(F13*96)=1 "Simples"! Cheer...

There must be a way.........
There must be a way to be sure all Vista and W7 system files are signed (not corrupted). I've researched and have not found a way. Can anybody help? <five256@NOwhere.com> wrote in message news:hb12r5tjthdtl87hlpgrcapq9b0hd87c4e@4ax.com... > There must be a way to be sure all Vista and W7 system files are signed > (not corrupted). I've researched and have not found a way. Can anybody > help? Yes, there is. It's called the System File Check tool. Read about system file protection here: http://support.microsoft.com/kb/222193 On Mon, 29 Mar 2...

Report Header to Include Search Date???
Here is my situation: I have a report that I am running off a query. When you run the report it prompts you to enter a date range you'd like to run the report for (One box pops up for start date and one pops up for end date). I would like to include the requested date range of the report in the header. Is there any way to do that?? On Fri, 22 Feb 2008 12:44:00 -0800, Amy B wrote: > Here is my situation: > I have a report that I am running off a query. When you run the report it > prompts you to enter a date range you'd like to run the report for (One box > pops ...

A list which don not include zero
Dear all I have in column A accounts. In column C i have the values. Let's say that all values should be zero. The lines are more than 15.000. I need to search all values in column C and find out if there is a value different from zero. I do not want ot use filters. I need a function in order to bring in a cell the status OK all values are zero. (as check box for the user). Any clever way to do it. Hi =ROUND(SUM(C:C),2)=0 -- Regards Roger Govier Manos wrote: > Dear all > > I have in column A accounts. In column C i have the values. Let's say t...