Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2006

Tip: Looking for answers? Try searching our database.

precompiled headers question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 31 Mar 2006 06:49 GMT
Hi,

I am writing some unmanaged c++ code that would need to be compiled in the
future using compilers other than vc++. I'm using the feature of vc++ "use
precompiled headers", is there going to be any problem if I compile this
code on other compilers? I'm asking this because when I use pch *some* of my
cpp files has following as there first line

#include "StdAfx.h"

but if I dont use pch, I have to change that to

#include "../StdAfx.h"

Regards,

Ab.
Bruno van Dooren - 31 Mar 2006 07:36 GMT
> I am writing some unmanaged c++ code that would need to be compiled in the
> future using compilers other than vc++. I'm using the feature of vc++ "use
[quoted text clipped - 8 lines]
>
> #include "../StdAfx.h"

Hi,

This will not be a problem. You can enable or disable the use of precompiled
headers in your project configuration properties. You don't have to change
anything to your sources at all.

Going to another compiler will not be a problem in that regard, but you have
to check whether you use microsoft specific features or not. you could /Za
to disable language extensions to insure that you only use ANSI.C++.

Btw GCC 4 also support precompiled headers I believe.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Abubakar - 31 Mar 2006 07:49 GMT
> headers in your project configuration properties. You don't have to change
> anything to your sources at all.

then why do I have to change the "stdafx.h" to "../stdafx.h" when I'm not
using pch?

Should I delete this #include "stdafx.h" from the start of my *cpp* files
when NOT using pch?

Ab.

> > I am writing some unmanaged c++ code that would need to be compiled in the
> > future using compilers other than vc++. I'm using the feature of vc++ "use
[quoted text clipped - 27 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"
Bruno van Dooren - 31 Mar 2006 08:50 GMT
>> headers in your project configuration properties. You don't have to
>> change
>> anything to your sources at all.
>
> then why do I have to change the "stdafx.h" to "../stdafx.h" when I'm not
> using pch?

You shouldn't have to. Just right click the file, select properties, go to
C++ ->precompiled headers.
there you can change the PCH usage.
You can also do this at project level.

> Should I delete this #include "stdafx.h" from the start of my *cpp* files
> when NOT using pch?

No, because most times it actually contains other headers.
Also, if the project is configured to use pch, you will get a compiler error
when you don't include it.
Using or not using PCH is a setting. It should not affect your sources at
all.

It is a technique for speeding up compilation times. nothing more.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Carl Daniel [VC++ MVP] - 31 Mar 2006 18:15 GMT
> Hi,
>
[quoted text clipped - 10 lines]
>
> #include "../StdAfx.h"

Then you have path problems in your project environment.

When you're compiling with PCH, the compiler completely ignores everything
in the file up through #include "stdafx.h".  The fact that you have to
change the path when you don't use PCH to me indicates that the compiler
can't find your stdafx.h in any of the standard places.

What does the directory structure of your project look like?  In particular,
where are stdafx.h and the file containing the #include in relation to one
another?

-cd
Abubakar - 03 Apr 2006 09:22 GMT
> What does the directory structure of your project look like?  In particular,
> where are stdafx.h and the file containing the #include in relation to one

All my source files are not in the same folder. I have made extra folders to
contain source and headers. For example, there is a folder called Publish,
which contains headers and sources of publishing functionality. When I added
these files to my project as new items, they had there first line as
"#include "stdafx.h"". That was the time when my project settings had pch
enabled. Than at some point I changed project settings to not using pch,
after which when I compiled the project I started getting these errors that
there was no stdafx.h. But I knew that stdafx.h is at the root of the source
tree, so I just changed it to "#include "../stdafx.h"" and it compiled fine,
which also got me thinking why was it (lets say blockpublishing.cpp)
compiling before when it had no stdafx.h on its path. (my lack of cpp
knowledge).

I have other problems as well, related to header files includes. Like I have
a common.h file that has some utility functions and some global variables
that I intend to use at more than one place. Now I include common.h at one
file, lets say one.h and use its functions which works ok, and than at some
other place,lets say two.h, I include the common.h and compiler starts
giving me errors, while linking the source, saying that
"something__proc___here" is already defined in "something.obj". However if I
dont include common.h in two.h, the two.h has no idea of the things (global
functions and variables) that I write, which I assume will me visible to
two.h that are inside the common.h. Now the next thing I'm planning to do is
inlcude my common.h in stdafx.h and see what happens. Also will play with
#pragma once stuff which looks insteresting and I hope its not microsoft
specific.

The purpose I wrote this second para above is:
Having a C# background, I think I really need to read some really good and
detailed article on the problems that a dev can face while working on c++
projects having to do with #include. It would be great if you or anyone else
has some web links on this, or maybe discuss here.

Ab.

> > Hi,
> >
[quoted text clipped - 23 lines]
>
> -cd
Bruno van Dooren - 03 Apr 2006 12:39 GMT
> > What does the directory structure of your project look like?  In
> particular,
[quoted text clipped - 12 lines]
> compiling before when it had no stdafx.h on its path. (my lack of cpp
> knowledge).

That is because the compiler will look for the PCH instead of trying to load
the real include file.

if you separate include and source files you have to add additional include
directories in your project settings. otherwise you have to explicitly tell
the compiler where those files are (by using ../..) and that removes
flexibility from your project layout.

> I have other problems as well, related to header files includes. Like I have
> a common.h file that has some utility functions and some global variables
[quoted text clipped - 6 lines]
> functions and variables) that I write, which I assume will me visible to
> two.h that are inside the common.h. Now the next thing I'm planning to do is

do it like this:
//common.h
extern int g_Global;
extern int MyFunction(void);

//common.cpp
#include "common.h"
int g_Global;
int MyFunction(void)
{
   //...
}

that way there is only 1 definition, and you can use them by simply
including common.h

> inlcude my common.h in stdafx.h and see what happens. Also will play with
> #pragma once stuff which looks insteresting and I hope its not microsoft
> specific.

another thing you'll see often is this:

#ifndef __SOME_INCLUDE_GUARD__
#define__SOME_INCLUDE_GUARD__

//declarations here

#endif

> The purpose I wrote this second para above is:
> Having a C# background, I think I really need to read some really good and
> detailed article on the problems that a dev can face while working on c++
> projects having to do with #include. It would be great if you or anyone else
> has some web links on this, or maybe discuss here.

to find out if something is MSFT specific, find the topic in the MSDN help
collection. it is always mentioned if something is ANSI or microsoft specific.

www.codeproject.com is always a good place to start when looking for info.
if you are serious about C++ you should buy a good book. there are several.
my favorite is still 'the C++ programming language' by stroustrub
it can be a bit dull at times, but it is complete.

if you have specific questions you can always ask them here also.

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Abubakar - 04 Apr 2006 06:30 GMT
> > > What does the directory structure of your project look like?  In
> > particular,
[quoted text clipped - 83 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"
Abubakar - 04 Apr 2006 06:37 GMT
> do it like this:
> //common.h
> extern int g_Global;
> extern int MyFunction(void);

This was very helpful.

Thanks for all other tips as well. My #include problems are settling slowly
n slowly :-)

Regards,

Abubakar.

> > > What does the directory structure of your project look like?  In
> > particular,
[quoted text clipped - 83 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.