简体   繁体   English

Visual Studio删除一个shared.pch文件,关于自定义构建步骤的问题

[英]Visual Studio deletes a shared .pch file, and questions about custom build steps

I try to use a shared.pch file, which is compiled in one project and used in others.我尝试使用 shared.pch 文件,该文件在一个项目中编译并在其他项目中使用。

However the.pch file is deleted if a.pdb filename of the PCH project differs from.pdb filenames of the other projects.但是,如果 PCH 项目的 .pdb 文件名与其他项目的 .pdb 文件名不同,.pch 文件将被删除。

This page doesn't answer the question: https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio/此页面未回答问题: https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio/

I don't want to use a same name for all PDBs.我不想对所有 PDB 使用相同的名称。

Questions:问题:

1) Why the.pch file is deleted at the start of other projects compilation, which leads to a C1083 error (.pch not found), if PDB names are not equal, not like in that page? 1)为什么在其他项目编译开始时.pch文件被删除,导致C1083错误(找不到.pch),如果PDB名称不相等,不像那个页面?

2) I copy pch.pdb and pch.idb files using COPY command, is there a RENAME comand or something, if the copied pch.pdb should be named just like a dependent project's PDB? 2) 我使用 COPY 命令复制 pch.pdb 和 pch.idb 文件,是否有 RENAME 命令或其他东西,如果复制的 pch.pdb 应该像依赖项目的 PDB 一样命名? And where can I find a complete list of Custom Build Step command?我在哪里可以找到自定义构建步骤命令的完整列表?

3) I don't understand the purpose of "Additional dependencies" and "Outputs" in Custom Build Step. 3) 我不明白自定义构建步骤中“附加依赖项”和“输出”的用途。 Can I enter the.pch filename into the dependency list, so it won't be deleted?我可以在依赖列表中输入.pch 文件名,这样它就不会被删除吗? Does the output list need to contain the dependent project's PDB name, or the pch.pdb, or both? output 列表是否需要包含依赖项目的 PDB 名称或 pch.pdb 或两者?

Did you use the sample code under the github link .您是否使用了github 链接下的示例代码。

If so, you should download and then use that sample and if you create your own project, you should check your projects carefully.如果是这样,您应该下载并使用该示例,如果您创建自己的项目,则应仔细检查您的项目。

Borrowing from this tutorial to your project, I think you need to pay attention to whether you have any additional custom targets in your xxx.vcxproj file to delete the PCH file.借用本教程到你的项目,我想你需要注意你的xxx.vcxproj文件中是否有任何额外的自定义目标来删除PCH文件。 Therefore, you need to check each xxx.vcxproj file carefully.因此,您需要仔细检查每个xxx.vcxproj文件。 In vs, there will be no deletion of certain files due to the different.pdb file names of the PCH project and other projects, so check whether there are additional operations of your own.在vs中,由于PCH项目和其他项目的.pdb文件名不同,不会删除某些文件,所以检查一下是否有自己的额外操作。

1) Why the.pch file is deleted at the start of other projects compilation, which leads to a C1083 error (.pch not found), if PDB names are not equal, not like in that page? 1)为什么.pch文件在其他项目编译开始时被删除,导致C1083错误(.pch not found),如果PDB名称不相等,不像那个页面?

First of All , make sure that there are no other option to delete PCH file in your projects.首先,确保没有其他选项可以删除项目中的 PCH 文件。

The PCH project is to create a PCH file and the other two projects are to use such file. PCH 项目是创建一个 PCH 文件,另外两个项目是使用这个文件。 And every time when you build the two projects(reference the PCH project), and always execute the build of PCH project and then build the two project.并且每次构建两个项目时(参考PCH项目),总是执行PCH项目的构建然后构建两个项目。 So the PCH is always created and later be used in two projects.所以 PCH 总是被创建,然后在两个项目中使用。

Based on it, you should ensure that all three projects create and use the same address for the file.基于此,您应该确保所有三个项目都为文件创建和使用相同的地址。

SharedPCH project SharedPCH 项目

在此处输入图像描述

ConsoleApplication1控制台应用程序1

在此处输入图像描述

ConsoleApplication2控制台应用程序2

在此处输入图像描述

In the sample code,the PCH file exists under SharedPchSample\Outputs\Intermediate\Shared\Win32\Debug .在示例代码中, PCH文件位于SharedPchSample\Outputs\Intermediate\Shared\Win32\Debug下。

2) I copy pch.pdb and pch.idb files using COPY command, is there a RENAME comand or something, if the copied pch.pdb should be named just like a dependent project's PDB? 2)我使用 COPY 命令复制 pch.pdb 和 pch.idb 文件,如果复制的 pch.pdb 应该像依赖项目的 PDB 一样命名,是否有 RENAME 命令或其他东西? And where can I find a complete list of Custom Build Step command?我在哪里可以找到自定义构建步骤命令的完整列表?

Custom Build Step is under every project--> Properties --> Custom Build Step --> Command Line , and then you can find it.That custom step is just CMD command.自定义构建步骤在每个项目下--> Properties -> Custom Build Step -> Command Line ,然后你就可以找到它。自定义步骤只是CMD命令。 And you can execute CMD in that to do extra opertion.您可以在其中执行 CMD 进行额外操作。

Besides , I guess you want to make those xxx.pdb and xxx.idb be the same name of the project name in order to distinguish one from another.此外,我猜你想让那些xxx.pdbxxx.idb与项目名称相同,以便相互区分。 You can right-click on every project--> Properties --> C/C++ --> Output Files --> Program Database File Name -->change it and to use $(IntDir)$(ProjectName).pdb .您可以右键单击每个项目 --> Properties --> C/C++ --> Output Files --> Program Database File Name --> 更改它并使用$(IntDir)$(ProjectName).pdb More about Custom Build Steps , you can refer to this link .有关自定义构建步骤的更多信息,您可以参考此链接

I don't understand the purpose of "Additional dependencies" and "Outputs" in Custom Build Step.我不明白自定义构建步骤中“附加依赖项”和“输出”的目的。 Can I enter the.pch filename into the dependency list, so it won't be deleted?我可以将.pch文件名输入到依赖列表中,这样它就不会被删除? Does the output list need to contain the dependent project's PDB name, or the pch.pdb, or both? output 列表是否需要包含依赖项目的 PDB 名称或 pch.pdb 或两者?

Additional dependencies is set to use the PCH file's content in the project 1 and 2 which is similar to configuring the address of a reference class library in a c++ project. Additional dependencies项设置为使用项目 1 和 2 中的 PCH 文件内容,这类似于在 c++ 项目中配置参考 class 库的地址。 And I think it might be redundant and since the author has add it which implies that it is well-founded.而且我认为这可能是多余的,因为作者添加了它,这意味着它是有根据的。

And Outputs is the author customized output path, the author changed the output address of the project, and started a new custom output path and a temporary output path. And Outputs is the author customized output path, the author changed the output address of the project, and started a new custom output path and a temporary output path.

Actually , the xxx.pch and its pdb and idb file will not be copied into outputpath.实际上,不会将xxx.pch及其pdbidb文件复制到 outputpath 中。 So the custom build step is to copied the files into temporary output path.所以自定义构建步骤是将文件复制到临时 output 路径中。 And if you want to copied them into the final outputpath, you can also use these in CustomBuildStep.targets file:如果你想将它们复制到最终的输出路径中,你也可以在CustomBuildStep.targets文件中使用它们:

<CustomBuildStep>
<Command>
if EXIST "$(SharedPdb)" xcopy /Y /F "$(SharedPdb)" "$(IntDir)"
if EXIST "$(SharedIdb)" xcopy /Y /F "$(SharedIdb)" "$(IntDir)"
if EXIST "$(SharedPdb)" xcopy /Y /F "$(SharedPdb)" "$(OutDir)"
if EXIST "$(SharedIdb)" xcopy /Y /F "$(SharedIdb)" "$(OutDir)"
</Command>
<Outputs>$(IntDir)vc$(PlatformToolsetVersion).pdb;</Outputs>
<Inputs>$(SharedPdb)</Inputs>
</CustomBuildStep>

And in fact, one project references another project, and the output files of the referenced project are automatically copied to the main project.而事实上,一个工程引用了另一个工程,被引用工程的output文件会自动复制到主工程中。 Perhaps this is because the author's SharePCH project does not generate the pdb and idb files, so those files of the dependent project will not be found in the main project.可能是因为作者的SharePCH项目没有生成pdb和idb文件,所以在主项目中找不到那些依赖项目的文件。

For some reason (I did this or not) the generated by compiler.pdb file was not $(PlatformToolsetVersion).pdb, but $(ProjectName).pdb.由于某种原因(我是否这样做),compiler.pdb 文件生成的不是 $(PlatformToolsetVersion).pdb,而是 $(ProjectName).pdb。 So the copied into other project folders shared.pdb file was pch.pdb in my case, while other projects were expecting different names.因此,在我的情况下,复制到其他项目文件夹 shared.pdb 文件是 pch.pdb ,而其他项目期望不同的名称。 And that was triggering a DELETE task in Microsoft.CppCommon.targets, ("Delete the pch file if the pdb file has been deleted.").这触发了 Microsoft.CppCommon.targets 中的 DELETE 任务,(“如果 pdb 文件已被删除,则删除 pch 文件。”)。 Instead of changing the output.pdb name I just looked into XCOPY command and made it to change the copied filename to an expected by a specific project (actually then I just added a custom Target with a renaming Copy task right into the project file instead of using the CustomBuildStep calling a xcopy OS's command, as now I learned more about MSBuild).而不是更改 output.pdb 名称,我只是查看了 XCOPY 命令并将复制的文件名更改为特定项目所期望的(实际上,我只是将带有重命名复制任务的自定义目标添加到项目文件中,而不是使用 CustomBuildStep 调用 xcopy 操作系统的命令,因为现在我了解了有关 MSBuild 的更多信息)。

Then I also changed the generated by Linker output.pdb, just added "Linked" suffix to the name, so there are no conflicts between Compiler's and Linker's PDBs.然后我也改了Linker生成的output.pdb,只是在名字后面加了“Linked”后缀,这样编译器和链接器的PDB就没有冲突了。 Not sure if that is a good idea to change the default settings without a big reason.不确定在没有很大原因的情况下更改默认设置是否是个好主意。

I guess it's better just to change the Compiler's output PDB to $(PlatformToolsetVersion).pdb, so all projects will use the same name.我想最好将编译器的 output PDB 更改为 $(PlatformToolsetVersion).pdb,这样所有项目都将使用相同的名称。

That was the first time I had looked into MSBuild and advanced project settings, now it seems to be obvious, that a project using a shared.pdb wants some familiar.pdb name, not a random pch.pdb那是我第一次查看 MSBuild 和高级项目设置,现在似乎很明显,使用 shared.pdb 的项目需要一些熟悉的.pdb 名称,而不是随机的 pch.pdb

Here is my custom Target imported into project files copying the shared.pdb only if it was rebuilt (.idb is not generated in my case):这是我导入到项目文件中的自定义目标,仅在重新构建时复制 shared.pdb(在我的情况下未生成 .idb):

<Target Name="CopyFreshPchPdb" BeforeTargets="ClCompile" 
    Inputs="$(PchDir)\pch.pdb"
    Outputs="$(IntDir)\$(ProjectName).pdb">
    <Message  Importance="High" Text="Copying shared pch.pdb" />
    <Copy
        SourceFiles="$(PchDir)\pch.pdb"
        DestinationFiles="$(IntDir)\$(ProjectName).pdb">
    </Copy>
</Target>

I'd like to add that I had a similar situation where a shared.pch file was being deleted but for a different reason related to the.pdb file.我想补充一点,我遇到过类似的情况,其中 shared.pch 文件被删除,但出于与 .pdb 文件相关的不同原因。

The reason was that the pdb formats were different across the exe and the PCH.lib.原因是 exe 和 PCH.lib 中的 pdb 格式不同。 The PCH library project has its 'Project Properties -> C/C++ -> General -> Debug Information Format' set to 'C7 compatible (/Z7)'. PCH 库项目的“项目属性 -> C/C++ -> 常规 -> 调试信息格式”设置为“C7 兼容 (/Z7)”。

When I added a new project exe that depended on the PCH library I had forgotten that new projects default to using 'Program Database (/Zi)' for its 'Debug Information Format'.当我添加一个依赖于 PCH 库的新项目 exe 时,我忘记了新项目默认使用“程序数据库 (/Zi)”作为其“调试信息格式”。

So now when the main project is being built and linked with the PCH it would delete the PCH.pch file and complain that the.PCH is missing.所以现在当主项目正在构建并与 PCH 链接时,它会删除 PCH.pch 文件并抱怨缺少 .PCH。

Having all projects with the same matching Debug Information Format was the fix to prevent the PCH from being deleted.让所有项目都具有相同的匹配调试信息格式是防止 PCH 被删除的修复方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSBuild 删除了一个 lib 文件,破坏了构建。 Visual Studio 工作正常 - MSBuild deletes one lib file, breaking the build. Visual Studio works fine 如何强制Qt Visual Studio加载项更新自定义生成步骤以创建MOC文件 - How to Force Qt Visual Studio Add-in to Update Custom Build Steps to Create MOC Files Visual Studio中的这些.pch和.ncb文件是什么? - What are these .pch and .ncb files in visual studio? Visual Studio 2015-配置自定义生成批处理文件 - Visual Studio 2015 - configuring a custom build batch file Visual Studio 2013中“每个文件”的自定义生成步骤在哪里? - Where is the “per file” Custom Build Step in Visual Studio 2013? 关于Boost共享内存的问题 - Questions about Boost shared memory 如何修复构建时丢失的 .pch 文件? - How to fix .pch file missing on build? 关于“Visual Studio 2015 和 Visual Studio 2017 之间的二进制兼容性”的问题 - Questions about "Binary Compatibility between Visual Studio 2015 and Visual Studio 2017" 有关自定义分配器的问题 - Questions about custom allocators Visual Studio 2019 调试器进入 header 文件而不是 cpp - Visual Studio 2019 debugger steps into header file instead of cpp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM