visual-studio 如何使用单个 Visual Studio 解决方案同时构建 x86 和 x64?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1574075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a single Visual Studio solution to build both x86 and x64 at the same time?
提问by PeteVasi
I've got an x86 Visual Studio solution with many project files in it. Some of the DLL files are designed to work as plug-ins to other applications on a user's system.
我有一个 x86 Visual Studio 解决方案,其中包含许多项目文件。某些 DLL 文件被设计为用作用户系统上其他应用程序的插件。
We're expanding some of the DLL files to be able to support 64-bit applications. I'd like to set up the solution/projects so that just hitting "Build"will build both the x86 and x64 versions of those DLL files. The solution contains both C++ and C# projects.
我们正在扩展一些 DLL 文件以支持 64 位应用程序。我想设置解决方案/项目,以便只需点击“构建”即可构建这些 DLL 文件的 x86 和 x64 版本。该解决方案包含 C++ 和 C# 项目。
I realize that "Batch Build" is capable of building both, though it would be more convenient if developers could just click the same button as they have previously and have all of the output DLL files generated.
我意识到“批量构建”能够构建两者,但如果开发人员可以像以前一样单击相同的按钮并生成所有输出 DLL 文件会更方便。
Here are a couple of the modifications that I've tried to a test project, but that I haven't gotten to work:
以下是我尝试对测试项目进行的一些修改,但我还没有开始工作:
I've tried modifying the <Target Name="AfterBuild">to try:
我试过修改<Target Name="AfterBuild">以尝试:
<Target Name="AfterBuild" Condition=" '$(Platform)' == 'x86' ">
<PropertyGroup>
<Platform>x64</Platform>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<CallTarget Targets="Build"/>
</Target>
But that results in the following error:
但这会导致以下错误:
C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(565,5): error MSB4006: There is a circular dependency in the target dependency graph involving target "Build".
C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(565,5): error MSB4006: 在涉及目标“Build”的目标依赖图中存在循环依赖。
I think my conditions will prevent infinite recursion, but I understand how MSBuild could not see it that way.
我认为我的条件会阻止无限递归,但我理解 MSBuild 怎么看不到它。
I've also tried:
我也试过:
<Project DefaultTargets="MyBuild86;MyBuild64" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
...
<Target Name="MyBuild86">
<PropertyGroup>
<Platform>x86</Platform>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<CallTarget Targets="Build"/>
</Target>
<Target Name="MyBuild64">
<PropertyGroup>
<Platform>x64</Platform>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<CallTarget Targets="Build"/>
</Target>
But my DefaultTargetsappears to be ignored from within the Visual Studio IDE.
但是我DefaultTargets似乎在 Visual Studio IDE 中被忽略了。
Last, I've tried creating a separate project that imports the first project:
最后,我尝试创建一个单独的项目来导入第一个项目:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform>x64</Platform>
<PlatformTarget>x64</PlatformTarget>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<OutputPath>..$(Configuration)\x64\</OutputPath>
<ProjectGuid>{A885CAC3-2BBE-4808-B470-5B8D482CFF0A}</ProjectGuid>
</PropertyGroup>
<Import Project="BuildTest.csproj" />
</Project>
And this so far has shown the most promise. However, Visual Studio seems to ignore my OutputPathsetting from this new project and instead outputs the EXE/DLL file to the path specified in the original project. There isn't any PropertyGroupblock that I can see that is being executed in the original project to override this, so I'm not sure what's happening.
到目前为止,这已显示出最大的希望。但是,Visual Studio 似乎忽略了我OutputPath在这个新项目中的设置,而是将 EXE/DLL 文件输出到原始项目中指定的路径。没有任何PropertyGroup我可以看到在原始项目中正在执行的块来覆盖它,所以我不确定发生了什么。
采纳答案by Todd
We do something similar to build core assemblies for .NET Compact Framework.
我们做了一些类似的事情来为.NET Compact Framework构建核心程序集 。
Try this:
试试这个:
<Target Name="AfterBuild">
<MSBuild Condition=" '$(Platform)' == 'x86' " Projects="$(MSBuildProjectFile)" Properties="Platform=x64;PlatFormTarget=x64" RunEachTargetSeparately="true" />
</Target>
回答by bogser
Importing a project in such manner works for me in Visual Studio 2010:
在 Visual Studio 2010 中以这种方式导入项目对我有用:
TestProject64.vcxproj
TestProject64.vcxproj
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="TestProject.vcxproj" />
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{B7D61F1C-B413-4768-8BDB-31FD464AD053}</ProjectGuid>
</PropertyGroup>
</Project>
TestProject64.vcxproj.filters
TestProject64.vcxproj.filters
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="TestProject.vcxproj.filters" />
</Project>
TestProject.vcxproj has two configurations defined inside: Release|x86and Release|x64. As you can see, TestProject64.vcxproj has only the Release|x64configuration. Defining of at least one configuration in TestProject64.vcxproj is necessary, otherwise Visual Studio will not be able to add TestProject64.vcxproj to a solution.
TestProject.vcxproj 内部定义了两个配置:Release|x86和Release|x64。如您所见,TestProject64.vcxproj 只有Release|x64配置。必须在 TestProject64.vcxproj 中定义至少一个配置,否则 Visual Studio 将无法将 TestProject64.vcxproj 添加到解决方案中。
Now it's possible to include both TestProject.vcxproj and TestProject64.vcxproj to the same solution and build Release|x86and Release|x64at the same time.
现在可以将 TestProject.vcxproj 和 TestProject64.vcxproj 包含到同一个解决方案中,并同时构建Release|x86和Release|x64。
回答by the_mandrill
I think the best way of doing this is to invoke MSBuild from the command line. It shouldn't need editing of MSBuild files. Just run
我认为最好的方法是从命令行调用 MSBuild。它不需要编辑 MSBuild 文件。赶紧跑
msbuild myproj.sln /p:Configuration="Debug|Win32"
msbuild myproj.sln /p:Configuration="Debug|x64"
I assume that if a developer is using Visual Studio then they'll only be generating the DLL files so they can debug with them, and that you have a separate build process if you're actually deploying the DLL files.
我假设如果开发人员使用 Visual Studio,那么他们只会生成 DLL 文件,以便他们可以使用它们进行调试,并且如果您实际部署 DLL 文件,则您有一个单独的构建过程。
回答by Leo Davidson
For C++, and if it's a project whose files/settings don't change often, one way to do it is create two projects within the solution, with both projects referring to the same source files. Then, in x64 builds, set one project to build 64-bit and the other 32-bit. (In x86 builds, set one as 32-bit and turn off the other.)
对于 C++,如果它是一个文件/设置不经常更改的项目,一种方法是在解决方案中创建两个项目,两个项目都引用相同的源文件。然后,在 x64 构建中,将一个项目设置为构建 64 位,另一个为 32 位。(在 x86 版本中,将一个设置为 32 位并关闭另一个。)
We've been using this for a while and it works fine.
我们已经使用了一段时间,它运行良好。
Of course, you have to be careful that any changes you make to one are also made to its copy. i.e. if you add/remove a file or change its build setting, you have to do it in two places. Source-code changes still only need to be done once, because there's still only one copy of each source file.
当然,您必须小心,您对其所做的任何更改也会对其副本进行。即,如果您添加/删除文件或更改其构建设置,则必须在两个地方进行。源代码更改仍然只需要完成一次,因为每个源文件仍然只有一个副本。
And, of course, you may decide that doing this is more complex/risky than switching away from using the IDE. In our case it's worked really well, though.
而且,当然,您可能会认为这样做比不使用 IDE 更复杂/风险更大。不过,在我们的案例中,它运行得非常好。
回答by Pieter Breed
You are not going to be able to do this with the UI of Visual Studio. For this you will need to hack the MSBuild files.
您将无法使用 Visual Studio 的 UI 执行此操作。为此,您需要破解 MSBuild 文件。
回答by Mike Pliam
Perhaps I've missed the point of this discussion.
也许我错过了这次讨论的重点。
Using Visual Studio, go to menu Build→ Configuration Manager. In the Active Solution Platformdrop down, select "New...", and a New Solution Platformdialog appears. Select x64 and accept the default Copy From. Close the dialog and the Configuration Manager.
使用 Visual Studio,转到菜单Build→ Configuration Manager。在Active Solution Platform下拉菜单中,选择“New...”,然后会出现一个New Solution Platform对话框。选择 x64 并接受默认的Copy From。关闭对话框和配置管理器。
Now open menu Build→ Batch Build. Check those configurations you want to build and build them. You will find the x64 build executables separate from the Win32 executable files.
现在打开菜单Build→ Batch Build。检查您要构建的配置并构建它们。您会发现 x64 构建可执行文件与 Win32 可执行文件是分开的。
You can verify that these are what was intended by right clicking on the executable files, selecting Properties, and select the Compatibilitytab. In the dropdown window you can check to see what operating systems the executable file can be run in.
您可以通过右键单击可执行文件,选择Properties,然后选择Compatibility选项卡来验证这些是否是预期的。在下拉窗口中,您可以查看可执行文件可以在哪些操作系统中运行。
Obviously, there may be some other tweaking you might have to do to get all the output files in their proper places, but this method seem somewhat simpler than fooling with build than those described above.
显然,您可能需要进行一些其他调整才能将所有输出文件放在适当的位置,但这种方法似乎比上述方法更简单。
回答by Paul B.
I would suggest to create a dummy C++ Makefile project and then invoke MSBuild twice from it:
我建议创建一个虚拟的 C++ Makefile 项目,然后从中调用 MSBuild 两次:
msbuild myproj.sln /p:Configuration="Debug|Win32"
msbuild myproj.sln /p:Configuration="Debug|x64"
回答by JayRu
I ran into this problem with a project running in Visual Studio 2008 (on Windows XP) (32-bit) and also Visual Studio 2010 (on Windows 7) (64-bit).
我在 Visual Studio 2008(在 Windows XP 上)(32 位)和 Visual Studio 2010(在 Windows 7 上)(64 位)中运行的项目中遇到了这个问题。
The solution I used was to use the $(PROGRAMFILES) variable. It resolved correctly on both machines.
我使用的解决方案是使用 $(PROGRAMFILES) 变量。它在两台机器上都正确解决。

