473,236 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

Precompiled header usage

Hi All,
I was wondering if it is possible to use precompiled headers without having
to include a <stdafx.h> or whatever in every source file.
My problem is that I have a project that makes heavy usage of a large third
party library which means after pre-processing each translation unit is
around 1meg in size. There are reasons though why I cannot use the standard
approach to precompiled headers.

What I would like to do is somthing like this:
1) create a header that includes all headers from third party library
2) compile this into a .pch file
3) have VS use this .pch file while compiling any source file it encounters

Is this possible?

Andy
Nov 16 '05 #1
4 2484

--------------------
From: "Andrew Ward" <an***@ihug.co.nz>
References: <ur**************@TK2MSFTNGP09.phx.gbl> <uw**************@TK2MSFTNGP12.phx.gbl>Subject: Re: Precompiled header usage
Date: Wed, 10 Sep 2003 17:26:22 +1200
Lines: 65
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <ea**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: 210-54-80-239.dialup.xtra.co.nz 210.54.80.239
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:28151
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hi Brandon,
The problem I am facing is that I cannot add #include "all.h" or whatever toeach source file in the project. About half of the source that is compiled
for the project is c++ generated from XML specifications, this source is allgenerated automatically in the build process.

An approach like the following would be great for my type of build
environment:

Each time the compiler encounters an #include <xyz.h> directive it checks a
cache to see if it has a precompiled version of the header 'xyz.pch' that isup to date, if one is found then it is used, otherwise a new precompiled
header is created 'xyz.pch' and put in the cache.

It seems to me that this approach would be more efficient as well, because
each file being compiled would only include the precompiled data that it
needs, instead of the norm of loading a huge precompiled header for every
source file, whether the file needs all the data or not.

Is anything of this sort possible?
"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
Andrew Ward wrote:
> Hi All,
> I was wondering if it is possible to use precompiled headers without
> having to include a <stdafx.h> or whatever in every source file.
> My problem is that I have a project that makes heavy usage of a large
> third party library which means after pre-processing each translation
> unit is around 1meg in size. There are reasons though why I cannot use
> the standard approach to precompiled headers.
>
> What I would like to do is somthing like this:
> 1) create a header that includes all headers from third party library
> 2) compile this into a .pch file
> 3) have VS use this .pch file while compiling any source file it
> encounters
>
> Is this possible?


Hi Andy,
What you'd like to do is very easy. To create a precompiled header,

simply
tell the compiler which header that should be. Use the /Yc command-line
switch to do that. Then when you want to use that precompiled header, use
the /Yu switch.

The precompiled header needs to be the first #include in the file.

The only reason that you would need to use stdafx.h for that is because
Visual Studio sets up projects to do this by default. You can of course
change that. Go to your project properties to change which file the

project
uses.

I hope that makes sense. Cheerio!

--
Brandon Bray Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Andy,

You seem to have misunderstood the VC++ PCH mechanism. The PCH is not a
more compact form of the header. It's really a dump of the compiler's
symbol table at the #include point in a source file. When compiling a file
with the same #include, the compiler can be made to load the PCH (really
the symbol table) the assumption being that the internal state is now the
same as if the #include had been processed at that point.

You can make the compiler use a different PCH for each compiland, but the
obvious advantage is when several compilands share a certain set of files.
Then a PCH gets built once and re-used multiple times in a build. Building
a PCH per file will save you time only if you're building the same file
over and over, but PCHs are typically very large running into several MB
for windows apps; writing a PCH out is expensive but loading it isn't since
it's just mapped into memory and paged in as needed. Hence the best
strategy to improve build times is to gather all headers into an "all.h"
header and build a PCH for this and use it in all the compilands. You'll
have to make changes to your codegen tool for this, but I'm sure you'll be
amazed at the performance gain.

--
Tanveer Gani, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Nov 16 '05 #2

"Tanveer Gani [MSFT]" <tg***@online.microsoft.com> wrote in message
news:qk**************@cpmsftngxa06.phx.gbl...

--------------------
From: "Andrew Ward" <an***@ihug.co.nz>
References: <ur**************@TK2MSFTNGP09.phx.gbl> <uw**************@TK2MSFTNGP12.phx.gbl>
Subject: Re: Precompiled header usage
Date: Wed, 10 Sep 2003 17:26:22 +1200
Lines: 65
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <ea**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: 210-54-80-239.dialup.xtra.co.nz 210.54.80.239
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:28151
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hi Brandon,
The problem I am facing is that I cannot add #include "all.h" or whatever

to
each source file in the project. About half of the source that is compiledfor the project is c++ generated from XML specifications, this source is

all
generated automatically in the build process.

An approach like the following would be great for my type of build
environment:

Each time the compiler encounters an #include <xyz.h> directive it checks acache to see if it has a precompiled version of the header 'xyz.pch' that

is
up to date, if one is found then it is used, otherwise a new precompiled
header is created 'xyz.pch' and put in the cache.

It seems to me that this approach would be more efficient as well, becauseeach file being compiled would only include the precompiled data that it
needs, instead of the norm of loading a huge precompiled header for every
source file, whether the file needs all the data or not.

Is anything of this sort possible?
"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
Andrew Ward wrote:
> Hi All,
> I was wondering if it is possible to use precompiled headers without
> having to include a <stdafx.h> or whatever in every source file.
> My problem is that I have a project that makes heavy usage of a large
> third party library which means after pre-processing each translation
> unit is around 1meg in size. There are reasons though why I cannot use > the standard approach to precompiled headers.
>
> What I would like to do is somthing like this:
> 1) create a header that includes all headers from third party library
> 2) compile this into a .pch file
> 3) have VS use this .pch file while compiling any source file it
> encounters
>
> Is this possible?

Hi Andy,
What you'd like to do is very easy. To create a precompiled header,

simply
tell the compiler which header that should be. Use the /Yc command-line
switch to do that. Then when you want to use that precompiled header, use the /Yu switch.

The precompiled header needs to be the first #include in the file.

The only reason that you would need to use stdafx.h for that is because
Visual Studio sets up projects to do this by default. You can of course
change that. Go to your project properties to change which file the

project
uses.

I hope that makes sense. Cheerio!

--
Brandon Bray Visual C++ Compiler This posting is provided AS IS with no warranties, and confers no rights.



Andy,

You seem to have misunderstood the VC++ PCH mechanism. The PCH is not a
more compact form of the header. It's really a dump of the compiler's
symbol table at the #include point in a source file. When compiling a file
with the same #include, the compiler can be made to load the PCH (really
the symbol table) the assumption being that the internal state is now the
same as if the #include had been processed at that point.

You can make the compiler use a different PCH for each compiland, but the
obvious advantage is when several compilands share a certain set of files.
Then a PCH gets built once and re-used multiple times in a build. Building
a PCH per file will save you time only if you're building the same file
over and over, but PCHs are typically very large running into several MB
for windows apps; writing a PCH out is expensive but loading it isn't

since it's just mapped into memory and paged in as needed. Hence the best
strategy to improve build times is to gather all headers into an "all.h"
header and build a PCH for this and use it in all the compilands. You'll
have to make changes to your codegen tool for this, but I'm sure you'll be
amazed at the performance gain.

--
Tanveer Gani, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


Hi Tanveer, thanks for your response. There is at least one compiler that
implements it's precompiled-header support in the way I described (gcc) ,
that is why I asked.
The problem with your "all.h" approach is that my software has to be
compiled on many platforms, using different compilers. Some of which have no
precompiled-header support. This means if I were to use the "all.h" method
the compile times for other compilers that have no pch support would
skyrocket.
I believe that something such as pch support should not govern the way you
write your code, the fact the MS' pch support requires you to write your
code in a non efficient way is bad design.
Andy
Nov 16 '05 #3
Andrew Ward wrote:
Hi Tanveer, thanks for your response. There is at least one compiler that
implements it's precompiled-header support in the way I described (gcc) ,
that is why I asked.
Hi Andy,
It would be helpful if you were able to provide more detail on what GCC
feature you are using (or provide a link to some place that has
documentation without a license). To my knowledge (from discussions with
people who use and work on GCC), pre-compiled headers are not yet a feature
on GCC. I could be wrong, and would enjoy learning more about it.
The problem with your "all.h" approach is that my software has to be
compiled on many platforms, using different compilers. Some of which have
no precompiled-header support. This means if I were to use the "all.h"
method the compile times for other compilers that have no pch support
would skyrocket.
This isn't necessarily true. If this approach were used under an #ifdef
dependent on _MSC_VER, you could write code that compiles efficiently on
several platforms. There are very few cross-platform libraries that do not
make adjustments for particular compilers.
I believe that something such as pch support should not govern the way you
write your code, the fact the MS' pch support requires you to write your
code in a non efficient way is bad design.


I don't particularly think what Tanveer suggested was a bad design. In some
regards, it prevents excessive refactoring that often gets developers in to
trouble when different definitions are introduced for the same type. In any
case, I hope you can try writing something dependent on _MSC_VER that allows
you to succeed at your task.

Cheerio!

--
Brandon Bray Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.
Nov 16 '05 #4
Brandon Bray [MSFT] wrote:
I don't particularly think what Tanveer suggested was a bad design. In some
regards, it prevents excessive refactoring that often gets developers in to
trouble when different definitions are introduced for the same type. In any
case, I hope you can try writing something dependent on _MSC_VER that allows
you to succeed at your task.


I think Microsoft's precompiled headers is a bad design. In my opinion,
a header file should #include exactly what it needs (no more, no less)
so that it can be compiled by itself. This puts all the #include
dependencies exactly where they should be. Precompiled headers
encourages a design where header files do not include anything (or very
little), but instead rely on certain things already having been included
earlier in the compilation.

It also bugs me that precompiled headers is not just an optimization,
but can change the semantics of the program being compiled. For
example, if a .cpp file starts with:

#define PRECOMPILED_HEADERS_OFF
#include "stdafx.h"

Then PRECOMPILED_HEADERS_OFF will be defined if the project doesn't use
precompiled headers, but won't be defined if precompiled headers are
turned on.

I understand that implementing precompiled headers correctly and
efficiently without the restrictions Microsoft imposes is an extremely
hard problem. (In a previous job I actually looked into doing it.) So
I don't begrudge Microsoft for not doing it. But because they encourage
bad design I don't use precompiled headers in any of my projects until
the compilation times become completely unbearable. (Fortunately, I
almost never have to include windows.h.)

--
David Olsen
qg********@yahoo.com

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Asfand Yar Qazi | last post by:
Hi, Now that GCC 3.4 has precompiled headers, I'm thinking I can stop using pimpls to speed up development time, as it may make life easier (declaring pimpls takes a long time...) What are...
1
by: JoeS | last post by:
Is there anyway to share a single pch file between projects in VC 7.0? I have 300+ projects each of which creates its own pch. All projects include the exact same header files in the precompiled...
0
by: Bruno van Dooren | last post by:
Hi, I am working on a dll that exports COM interfaces, and i am having some troubles with the use of precompiled headers. the project compiles always, but if i use /Yu (default: use precompiled...
2
by: Tommy Vercetti | last post by:
I am working on a Managed C++ project and I get the following error: TestThread.cpp(3) : error C2859: c:\projects\ProjectName\debug\vc70.pdb is not the pdb file that was used when this...
0
by: citizenkahn | last post by:
I read an article entitled "Sharing precompiled headers between projects" on this group. I am in the same situation as the requesting author, but the solution has not worked for me. Here is my...
1
by: Alvo von Cossel I | last post by:
yo, i have a simple hello world win32 console application. it should work but there is an error. here is the most important part of the error: have you forgotten to add #include <stdfx.h> to...
3
by: Kevin Frey | last post by:
I am working on a test migration of our project to Visual Studio 2005 Beta 2 as a precursor to the availability of the full release of VS2005. The most onerous problem so far concerns the...
0
by: simon.hudon | last post by:
Hi everyone, I have a strange problem with precompiled headers with VC8. I have the problem while trying to use a precompiled header to build another precompiled header. I tried the sample...
11
by: gumboots | last post by:
Hi there guys, I've recently purchased "Sam's Teach Yourself C++ Fifth Edition" (About a week before the 6th edition came out) I'm trying to work through the book, but in trying to compile Hello...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.