.net 如何强制 MSBuild 编译为 32 位模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1074654/
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 do I force MSBuild to compile for 32-bit mode?
提问by Eric Nicholson
I'm using MSBuild (via NAnt) to compile a bunch of VB.NETassemblies. Because these assemblies depend on COM Interop, I need to guarantee that they run in 32-bit mode on 64 bit OS's. I can get the executable assemblies to compile to 32-bit by changing the project in Visual Studio, but I'd really like to be able to force all of the executables to be compiled to 32 bit mode on the build server.
我正在使用 MSBuild(通过NAnt)来编译一堆VB.NET程序集。因为这些程序集依赖于 COM Interop,所以我需要保证它们在 64 位操作系统上以 32 位模式运行。我可以通过在 Visual Studio 中更改项目来将可执行程序集编译为 32 位,但我真的希望能够在构建服务器上强制将所有可执行文件编译为 32 位模式。
I've tried a number of command-line parameters to MSBuild with no luck:
我已经尝试了许多 MSBuild 的命令行参数,但都没有成功:
- /p:Platform=win32
- /p:Platform=x86
- /p:ProcessorArchitecture=x86
- /p:平台=win32
- /p:平台=x86
- /p:处理器架构=x86
What am I doing wrong? Is there some reference to the properties that MSBuild uses when compiling VB projects?
我究竟做错了什么?在编译 VB 项目时,是否有对 MSBuild 使用的属性的一些参考?
采纳答案by JaredPar
If the assemblies themselves are always going to be 32 bit, why not add the setting to the .vbproj file? That will take MSBuild out of the equation.
如果程序集本身总是 32 位,为什么不将设置添加到 .vbproj 文件?这将使 MSBuild 脱离等式。
Just add the following line to the initial PropertyGroup in the .vbproj file
只需将以下行添加到 .vbproj 文件中的初始 PropertyGroup
<PlatformTarget>x86</PlatformTarget>
回答by Nader Shirazie
According to MSDN, you're doing the right thing. Looks like /p:Platform=x86, but actually, maybe it's /p:PlatformTarget=x86.
根据MSDN,您在做正确的事情。看起来像/p:Platform=x86,但实际上,也许是/p:PlatformTarget=x86。
Try to just invoke MSBuild directly with that parameter (make sure it's not an issue with your NAnt file. Look at the build output for the right build configuration (Debug/ Release).
尝试直接使用该参数调用 MSBuild(确保它不是您的 NAnt 文件的问题。查看正确构建配置(Debug/ Release)的构建输出。
回答by MrHam
Just in case this helps, I used this commandline to build my x86 platform target:
以防万一这有帮助,我使用这个命令行来构建我的 x86 平台目标:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe my.sln /t:build /p:Configuration=Release;Platform=x86
回答by Richard Berg
In Solution Explorer, right click the root node → Configuration Manager. You need to define a solution-wide configuration that instructs each project within to build as 32-bit. (Note: you probably already have one if you've ever set at least one project to build as 32-bit.) For a step-by-step walkthrough, see the MSDN blog post Solution Configurations.
在解决方案资源管理器中,右键单击根节点 →配置管理器。您需要定义一个解决方案范围的配置,指示其中的每个项目构建为 32 位。(注意:如果您曾经将至少一个项目设置为 32 位构建,那么您可能已经有了一个。)有关分步演练,请参阅 MSDN 博客文章解决方案配置。
Then, you specify the desired "platform" and "flavor" in your Team Build.proj/ .targetsfiles. For example:
然后,在Team Build.proj/.targets文件中指定所需的“平台”和“风格” 。例如:
<ConfigurationToBuild Include="Release|x86">
<FlavorToBuild>Release</FlavorToBuild>
<PlatformToBuild>x86</PlatformToBuild>
</ConfigurationToBuild>
You can specify more than one of these property sections to have several combinations built. I would copy/paste the "Release|x86" string (or whatever it looks like) directly from your .slnfile to ensure it matches exactly — you can't get it directly from Solution Explorer.
您可以指定多个属性部分以构建多个组合。我会直接从您的.sln文件中复制/粘贴“Release|x86”字符串(或任何看起来像的东西)以确保它完全匹配——您无法直接从解决方案资源管理器中获取它。
Regarding your comment:
关于你的评论:
MSBuild property evaluation is pretty complex since it mixes declarative and imperative styles. See the blog post MSBuild Property Evaluationfor details. I prefer not to rely on its subtleties.
MSBuild 属性评估非常复杂,因为它混合了声明式和命令式样式。有关详细信息,请参阅博客文章MSBuild 属性评估。我不想依赖它的微妙之处。
It's true that properties specified on the command line should override everything else, but Team Build has another layer of complexity. The ComputeConfigurationList task is called repeatedly via a recursive MSBuild invokation, not as an ordinary task. The way it pulls this off is to take the ordinary properties like PlatformToBuildand wrap them in a set of global properties called ConfigurationToBuild.PlatformToBuild(etc.) which are generated on the fly, once for each configuration. This makes the Team Build engine much more flexible internally, but it also makes hacking the command line behavior you want harder.
在命令行上指定的属性确实应该覆盖其他所有内容,但 Team Build 具有另一层复杂性。ComputeConfigurationList 任务通过递归 MSBuild 调用重复调用,而不是作为普通任务。它实现这一点的方法是将普通属性(例如)PlatformToBuild包装在一组称为ConfigurationToBuild.PlatformToBuild(等)的全局属性中,这些属性是动态生成的,每个配置一次。这使得 Team Build 引擎在内部更加灵活,但也使得破解您想要的命令行行为变得更加困难。
You could try setting ConfigurationToBuild.PlatformToBuildon the command line directly — it might work, I'm not sure. But it will definitely prevent you from ever building more than one configuration in a single build definition. For this reason, I'm sticking with my advice above.
您可以尝试ConfigurationToBuild.PlatformToBuild直接在命令行上进行设置——它可能会起作用,我不确定。但它肯定会阻止您在单个构建定义中构建多个配置。出于这个原因,我坚持我上面的建议。
回答by Jeb
After experiencing the exact same issue, I switched from using the version of MSBuild at C:\WINDOWS\Microsoft.NET\Framework64... to the version at C:\WINDOWS\Microsoft.NET\Framework(no 64) and things compiled just fine.
在遇到完全相同的问题后,我从使用 MSBuild 的版本切换C:\WINDOWS\Microsoft.NET\Framework64到了C:\WINDOWS\Microsoft.NET\Framework(no 64)的版本,并且编译得很好。
回答by Turker Tunali
For MSBuild version 15 it is /p:PlatformTarget=x86
对于 MSBuild 版本 15,它是 /p:PlatformTarget=x86
回答by dwonisch
The Nant msbuild-Task answer to this question:
Nant msbuild-Task 对这个问题的回答:
<msbuild project="your.sln">
<property name="PlatformTarget" value="x86" />
</msbuild>
回答by Soundararajan
A more practical way i use to find the right property is by opening one of the .csproj project file (in case of c#) and see the property that is affected when you select "x64" / "AnyCPU" / "x86" from Visual stdio. Whatever property that is changed, you need to set that from command line. With Visual studio 2015 it seems to be <Platform>. So you can invoke msbuild with the argument /p:Platform=x64and it should work.
我用来查找正确属性的一种更实用的方法是打开 .csproj 项目文件之一(在 c# 的情况下),并查看从 Visual 中选择“x64”/“AnyCPU”/“x86”时受影响的属性标准输出。无论更改什么属性,您都需要从命令行进行设置。对于 Visual Studio 2015,它似乎是 <Platform>。因此,您可以使用参数/p:Platform=x64调用 msbuild并且它应该可以工作。

