Visual Studio C#项目在从调试切换到发布时强制重建,反之亦然

时间:2015-10-05 19:13:54

标签: c# visual-studio visual-studio-2012 rebuild

似乎Visual Studio 2012 C#项目每次从Debug切换到Release时都会强制重建,反之亦然,即使项目中没有任何更改也是如此。对于独立项目,这不会发生 - 但如果您将一个项目引用添加到类库,则每次更改为不同的构建配置时,它都会开始显示该行为。每当配置选择发生变化时,我都会将其跟踪到force.build文件(obj\Debugobj\Release)的生成。为什么是这样?可以禁用吗?我认为切换配置本身不应该需要重建。我找不到任何提及“force.build”文件和/或如何在任何文档中禁用此“功能”。

重新创建问题:

  1. 点击File / New Project...
  2. 创建新的Visual C# - > Console Application。保持生成的代码不变。
  3. 右键点击解决方案资源管理器中的新解决方案,然后选择Add - > New Project...
  4. 选择Visual C# - > Class Library。保持生成的代码不变。
  5. 右键点击解决方案资源管理器中的ConsoleApplication1,然后选择Add Reference...
  6. 选中ClassLibrary1中的Solution旁边的框 - > Projects并点击OK
  7. Debug配置构建项目。正如预期的那样构建它。
  8. 切换到Release配置并重新构建。正如预期的那样构建它。
  9. 切换回Debug并重新构建。它构建,它不应该。它已经建好了,对吗?我们所做的就是更改目标配置选择。
  10. 这是一个错误吗?有解决方法吗?

2 个答案:

答案 0 :(得分:2)

我承认我从来没有过多关注它,但我只是为了好奇而尝试(Visual Studio 2012 Update 4)。

使用您描述的单个项目,我尝试切换配置,并且显然,当我更改配置时,主项目再次构建:

========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

但是,如果我查看输出文件夹,没有文件正在更新(每个文件的更改时间与之前的版本相同),除了vshost.exe(我禁用了它,但得到了相同的消息) 。然后我尝试将构建日志设置为更详细(工具 - 选项项目和解决方案 - 构建和运行 - MSBuild项目构建输出详细程度 - 从“最小”更改为“正常”或“详细”):

enter image description here

然后我得到以下输出:

1>------ Build started: Project: ConsoleApplication1, Configuration: Release Any CPU ------
1>Build started 06/10/2015 00:26:50.
1>GenerateTargetFrameworkMonikerAttribute:
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>CoreCompile:
1>Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
1>_CopyAppConfigFile:
1>Skipping target "_CopyAppConfigFile" because all output files are up-to-date with respect to the input files.
1>CopyFilesToOutputDirectory:
1>  ConsoleApplication1 -> c:\users\user\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Release\ConsoleApplication1.exe
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.09
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

所以看起来这个项目并没有真正得到每次重建。如果您发现所有内容都被重建(检查输出目录文件上的更改时间),那么尝试按照我的描述增加构建日志详细程度,看看发生了什么。希望它有所帮助。

答案 1 :(得分:0)

这确实似乎是一个错误(VS 2017 和 VS 2019):
https://developercommunity.visualstudio.com/t/changing-solution-configuration-rebuilds-dependent/726161

切换配置(build.force/objx86/x64)时会在 Debug 文件夹中创建一个文件 Release。这个文件的创建是错误的。上面的链接描述了问题和 Microsoft 的响应(“不会修复,请移至 .Net Core”)。