如何禁用 Visual Studio 的默认构建并将其替换为自定义构建工具?
Posted
技术标签:
【中文标题】如何禁用 Visual Studio 的默认构建并将其替换为自定义构建工具?【英文标题】:How can I disable Visual Studio's default build and replace it with a custom build tool? 【发布时间】:2019-08-31 23:22:07 【问题描述】:我正在尝试使用 Ninja 在 Visual Studio 中构建我的 C++ 项目,并且我有一个基本设置来编译和链接我已经成功测试过的命令行。
我现在正在尝试将其集成到 Visual Studio 中,并且我想用此自定义构建工具完全替换默认构建,同时仍将我的项目设置考虑在内。当我右键单击我的项目并单击构建时,我希望它运行我在项目属性中设置的这个自定义构建工具。目前它似乎在进行常规构建并完全忽略了自定义构建步骤。如果我右键单击我的自定义构建文件并单击编译,那么它将执行自定义构建步骤。
自定义构建输出 - 当我右键单击并编译自定义构建工具文件时
1>------ Build started: Project: test, Configuration: TEST x64 ------
1>Performing Custom Build Tools
1>[1/1] cl /Fo:build\obj\ /EHsc test.cpp test2.cpp
1>Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>test.cpp
1>test2.cpp
1>Generating Code...
1>Microsoft (R) Incremental Linker Version 14.16.27024.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>/out:test.exe
1>build\obj\test.obj
1>build\obj\test2.obj
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
全部重建项目
1>------ Rebuild All started: Project: test, Configuration: TEST x64 ------
1>Performing Custom Build Tools
1>[1/1] cl /Fo:build\obj\ /EHsc test.cpp test2.cpp
1>Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>test.cpp
1>test2.cpp
1>Generating Code...
1>Microsoft (R) Incremental Linker Version 14.16.27024.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>/out:test.exe
1>build\obj\test.obj
1>build\obj\test2.obj
1>pch.cpp
1>test.cpp
1>test2.cpp
1>Generating Code...
1>test.vcxproj -> C:\Users\user\source\repos\test\x64\TEST\test.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
对于后一个输出,看起来它执行了两次构建。一次使用 Visual Studio(我不想要),一次使用自定义构建工具。
有没有一种方法可以让我在我的项目上单击构建,它会仅执行我的自定义构建?
【问题讨论】:
【参考方案1】:虽然我认为这不是正确的解决方案,但我发现了一些可行的方法。
您可以编辑.vcxproj
文件以删除所有ClCompile
命令。
之前
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='TEST|x64'">
<ClCompile>
<AdditionalOptions />
</ClCompile>
<CustomBuild>
<Command>"C:\Users\user\Desktop\ninja.exe" -f build.ninja</Command>
<Outputs>build/obj</Outputs>
</CustomBuild>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
<ClInclude Include="test-private-pch.h" />
<ClInclude Include="test-public-pch.h" />
<ClInclude Include="test2.h" />
</ItemGroup>
<ItemGroup>
<ClCompileInclude="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClInclude>
<ClCompileInclude Include="test.cpp" />
<ClCompileInclude Include="test2.cpp" />
</ItemGroup>
之后
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='TEST|x64'">
<!-- <ClCompile>
<AdditionalOptions />
</ClCompile> -->
<CustomBuild>
<Command>"C:\Users\user\Desktop\ninja.exe" -f build.ninja</Command>
<Outputs>build/obj</Outputs>
</CustomBuild>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
<ClInclude Include="test-private-pch.h" />
<ClInclude Include="test-public-pch.h" />
<ClInclude Include="test2.h" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClInclude>
<ClInclude Include="test.cpp" />
<ClInclude Include="test2.cpp" />
</ItemGroup>
将 .cpp 文件上的 ClCompile
更改为 ClInclude
意味着它们在项目中仍然可见并且可以更改。
更改后重建所有输出
1>------ Rebuild All started: Project: test, Configuration: TEST x64 ------
1>Performing Custom Build Tools
1>[1/1] cl /Fo:build\obj\ /EHsc test.cpp test2.cpp
1>Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>test.cpp
1>test2.cpp
1>Generating Code...
1>Microsoft (R) Incremental Linker Version 14.16.27024.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>/out:test.exe
1>build\obj\test.obj
1>build\obj\test2.obj
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
现在,当我重建这个单一项目时,只有我的自定义构建会运行。如果有人对此有更好的解决方案,请随时发布答案。
【讨论】:
以上是关于如何禁用 Visual Studio 的默认构建并将其替换为自定义构建工具?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Visual Studio 2017 上禁用代码分析?
如何在 Visual Studio 2017 Web Proj ASP.NET Core 2.0 中禁用 Https
如何在Visual Studio中禁用ReSharper并再次启用它?