Microsoft KB Archive/194615

= FIX: A project deletes a precompiled header that was created by another project =

Article ID: 194615

Article Last Modified on 9/23/2005

-

APPLIES TO


 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q194615



SYMPTOMS
If one project uses a precompiled header (using compiler option /Yu) that was created by another project, then the precompiled header is deleted when you perform a Clean or Rebuild-All command in Developer Studio.



RESOLUTION
In the workarounds that follow, a YC project refers to the project that creates the precompiled header (PCH); a YU project refers to a project that uses, but does not create, the precompiled header.

Option 1
Use a custom build step to create the PCH file. This does not impact the YU projects.

In this case you can modify Project Settings for the Stdafx.cpp file so that it is built with custom build rules rather than the standard method for building the PCH file.

To do this from the YC project:  On the Project menu, click Settings to bring up the Project Settings dialog box. From the tree view in the left window, select Stdafx.cpp in the Source Files folder. Click the General tab in the right window and select Always use custom build step. Click the Custom Build tab.  In the Commands window, enter the following (where Yc.pch is the name of your project pre-compiled header file):

  attrib -r $(OutDir)\yc.pch cl [compiler switches] /Yc"stdafx.h" stdafx.cpp attrib +r $(OutDir)\yc.pch   In the Outputs window, enter the following (where Yc.pch is the name of your project pre-compiled header file):

<pre class="fixed_text">  $(OutDir)\yc.pch $(OutDir)\stdafx.obj </li>  Click Dependencies. Type the following in the user-defined Dependencies window:

<pre class="fixed_text">  stdafx.h                        </li> Click OK.</li></ol>

Notes <ul> You get the following error at the beginning of your Rebuild-All or Clean:

Error: Could not delete file "filename" : Access is denied.

</li> The pre-compiled header is not deleted even in the YC project when a Clean or Rebuild-All command is executed in Developer Studio. This behavior is consistent with Microsoft Visual C++, version 5.0.</li></ul>

Option 2
Use an environment variable to set the PCH switch in the YU projects. This does not impact the YC project(s). <ol> In the Windows NT Setting, Control Panel applet, select the System icon. Then select the Environment tab.</li> In the Variable field, type in YCREL .</li>  In the Value field, enter: <pre class="fixed_text">  /Fp"..\Release/yc.pch" /Yu"stdafx.h" (This uses relative paths although it is not required). Note that you also need an environment variable for the debug build, such as YCDBG. That is, you need two environment variables for each project that produces PCH files.

Note In Windows 95 or Windows 98, you must make system-wide environment variable setting changes by adding appropriate MS-DOS SET commands to the Autoexec.bat file and rebooting. </li> In the Project Settings dialog box for the YU project, select the main node of the project labeled YU in the file tree.</li> In the C/C++ tab, select the Precompiled Headers category.</li> Click Not using precompiled headers. This turns off the /Fp and /Yu compiler switches.</li> In the Project Options window (at the bottom of the Project Settings dialog box) add $(YCDBG) for your debug build or $(YCREL) for the Release build.</li> Click OK.</li> If you have not restarted Developer Studio since setting the environment variables, you must close it and reopen it.</li></ol>

Notes
 * You can unintentionally delete or modify the environment settings.
 * Build settings are hidden, which may make trouble-shooting build-related problems more difficult.

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This bug was corrected in Visual Studio 6.0 Service Pack 3.

<div class="moreinformation_section">

Steps to reproduce the behavior

 * 1) Create two MFC AppWizard DLL projects in the same workspace.
 * 2) Switch to the Win32 Release configuration for each project.
 * 3) In Project2, delete the Stdafx.cpp file from the project.
 * 4) In Project2, in the Project Settings dialog box, change the /Fp option to point to the PCH file from Project1.
 * 5) Build Project1.
 * 6) Build Project2.
 * 7) Rebuild All Project2 only.

Result

fatal error C1083: Cannot open precompiled header file: 'filename': No such file or directory

<div class="references_section">