将NASM汇编器集成到Visual Studio中

来源:互联网 发布:java堆栈 编辑:程序博客网 时间:2024/06/10 04:43
 243人阅读 评论(1) 收藏 举报

之前在《Visual Studio中使用NASM编译汇编文件》中介绍了如何将NASM汇编器集成到VS2005和VS2008中,但VS2010与VS2012的“生成自定义”与VS2005和VS2008的配置文件不一样了,需要重新进行配置。笔者在《Integrating a compiler/assembler in VS ; Using NASM with Visual Studio 2010》一文中找到了相应的配置文件,并简单作了一点界面上的汉化(后面将附上其内容)。

VS2010VS2012中有三个配置文件控制自定义生成规则,分别是*.props*.targets*.xml

如果是VS2010,将上面三个文件放到Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations目录中;如果是VS2012,则放在 Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations目录中,然后在VS中如图所示中 选择生成自定义,再勾选NASM


后面只要项目中有asm文件,则会自动选择NASM作为其默认编译器,如下图所示:


至此,我们可以使用集成在VSNASM编译器编译外联汇编了。但是有一个问题就是NASM默认的错误输出格式为GNU风格的,在VS中不能使用其直接定位到错误文件行。如下图:


 为了使VS能直接提示错误并可定位到文件件行,需要设置为错误格式为VC风格:


这样,在编译错误时会提示如下:


至此,我们可以尽情的使用NASM编译外联汇编文件了,使用起来与C/C++一样的感觉,最后将三个配置文件附上:

nasm.props文件内容:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  3.   <PropertyGroup  
  4.     Condition="'$(NASMBeforeTargets)' == '' and '$(NASMAfterTargets)' == '' and '$(ConfigurationType)' != 'Makefile'">  
  5.     <NASMBeforeTargets>Midl</NASMBeforeTargets>  
  6.     <NASMAfterTargets>CustomBuild</NASMAfterTargets>  
  7.   </PropertyGroup>  
  8.   <ItemDefinitionGroup>  
  9.     <NASM>  
  10.       <OutputFormat>$(IntDir)%(FileName).obj</OutputFormat>  
  11.       <Outputswitch>0</Outputswitch>  
  12.       <PackAlignmentBoundary>0</PackAlignmentBoundary>  
  13.       <CommandLineTemplate Condition="'$(Platform)' == 'Win32'">nasm [AllOptions] [AdditionalOptions]  %(Filename)%(Extension) </CommandLineTemplate>  
  14.       <CommandLineTemplate Condition="'$(Platform)' == 'X64'">nasm [AllOptions]  [AdditionalOptions]  %(Filename)%(Extension)</CommandLineTemplate>  
  15.       <CommandLineTemplate Condition="'$(Platform)' != 'Win32' and '$(Platform)' != 'X64'">echo NASM not supported on this platform</CommandLineTemplate>  
  16.       <ExecutionDescription>%(FileName).asm</ExecutionDescription>  
  17.     </NASM>  
  18.   </ItemDefinitionGroup>  
  19. </Project>  

nasm.targets文件内容:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  3.   <ItemGroup>  
  4.     <PropertyPageSchema  
  5.       Include="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml" />  
  6.     <AvailableItemName Include="NASM">  
  7.       <Targets>_NASM</Targets>  
  8.     </AvailableItemName>  
  9.   </ItemGroup>  
  10.   <PropertyGroup>  
  11.     <ComputeLinkInputsTargets>  
  12.       $(ComputeLinkInputsTargets);  
  13.       ComputeNASMOutput;  
  14.     </ComputeLinkInputsTargets>  
  15.     <ComputeLibInputsTargets>  
  16.       $(ComputeLibInputsTargets);  
  17.       ComputeNASMOutput;  
  18.     </ComputeLibInputsTargets>  
  19.   </PropertyGroup>  
  20.   <UsingTask  
  21.     TaskName="NASM"  
  22.     TaskFactory="XamlTaskFactory"  
  23.     AssemblyName="Microsoft.Build.Tasks.v4.0">  
  24.     <Task>$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml</Task>  
  25.   </UsingTask>  
  26.   <Target  
  27.     Name="_NASM"  
  28.     BeforeTargets="$(NASMBeforeTargets)"  
  29.     AfterTargets="$(NASMAfterTargets)"  
  30.     Condition="'@(NASM)' != ''"  
  31.     Outputs="%(NASM.OutputFormat)"  
  32.     Inputs="%(NASM.Identity);%(NASM.AdditionalDependencies);$(MSBuildProjectFile)"  
  33.     DependsOnTargets="_SelectedFiles">  
  34.     <ItemGroup Condition="'@(SelectedFiles)' != ''">  
  35.       <NASM Remove="@(NASM)" Condition="'%(Identity)' != '@(SelectedFiles)'" />  
  36.     </ItemGroup>  
  37.     <ItemGroup>  
  38.       <NASM_tlog Include="%(NASM.OutputFormat)" Condition="'%(NASM.OutputFormat)' != '' and '%(NASM.ExcludedFromBuild)' != 'true'">  
  39.         <Source>@(NASM, '|')</Source>  
  40.       </NASM_tlog>  
  41.     </ItemGroup>  
  42.     <Message  
  43.       Importance="High"  
  44.       Text="%(NASM.ExecutionDescription)" />  
  45.     <WriteLinesToFile  
  46.       Condition="'@(NASM_tlog)' != '' and '%(NASM_tlog.ExcludedFromBuild)' != 'true'"  
  47.       File="$(IntDir)$(ProjectName).write.1.tlog"  
  48.       Lines="^%(NASM_tlog.Source);@(NASM_tlog->'%(Fullpath)')"/>  
  49.     <NASM  
  50.       Condition="'@(NASM)' != '' and '%(NASM.ExcludedFromBuild)' != 'true'"  
  51.       Inputs="%(NASM.Inputs)"  
  52.       OutputFormat="%(NASM.OutputFormat)"  
  53.       Outputswitch="%(NASM.Outputswitch)"  
  54.       AssembledCodeListingFile="%(NASM.AssembledCodeListingFile)"  
  55.       GenerateDebugInformation="%(NASM.GenerateDebugInformation)"  
  56.       ErrorReporting="%(NASM.ErrorReporting)"  
  57.       IncludePaths="%(NASM.IncludePaths)"  
  58.       PreprocessorDefinitions="%(NASM.PreprocessorDefinitions)"  
  59.       UndefinePreprocessorDefinitions="%(NASM.UndefinePreprocessorDefinitions)"  
  60.       ErrorReportingFormat="%(NASM.ErrorReportingFormat)"  
  61.       TreatWarningsAsErrors="%(NASM.TreatWarningsAsErrors)"  
  62.       floatunderflow="%(NASM.floatunderflow)"  
  63.       macrodefaults="%(NASM.macrodefaults)"  
  64.       user="%(NASM.user)"  
  65.       floatoverflow="%(NASM.floatoverflow)"  
  66.       floatdenorm="%(NASM.floatdenorm)"  
  67.       numberoverflow="%(NASM.numberoverflow)"  
  68.       macroselfref="%(NASM.macroselfref)"  
  69.       floattoolong="%(NASM.floattoolong)"  
  70.       orphanlabels="%(NASM.orphanlabels)"  
  71.       CommandLineTemplate="%(NASM.CommandLineTemplate)"  
  72.       AdditionalOptions="%(NASM.AdditionalOptions)"  
  73.  />  
  74.   </Target>  
  75.   <Target  
  76.     Name="ComputeNASMOutput"  
  77.     Condition="'@(NASM)' != ''">  
  78.     <ItemGroup>  
  79.       <Link Include="@(NASM->Metadata('OutputFormat')->Distinct()->ClearMetadata())" Condition="'%(NASM.ExcludedFromBuild)' != 'true'"/>  
  80.       <Lib Include="@(NASM->Metadata('OutputFormat')->Distinct()->ClearMetadata())" Condition="'%(NASM.ExcludedFromBuild)' != 'true'"/>  
  81.     </ItemGroup>  
  82.   </Target>  
  83. </Project>  

nasm.xml文件内容:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ProjectSchemaDefinitions xmlns="http://schemas.microsoft.com/build/2009/properties" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">  
  3.   <Rule  
  4.     Name="NASM"  
  5.     PageTemplate="tool"  
  6.     DisplayName="Netwide Assembler"  
  7.     Order="200">  
  8.     <Rule.DataSource>  
  9.       <DataSource  
  10.         Persistence="ProjectFile"  
  11.         ItemType="NASM" />  
  12.     </Rule.DataSource>  
  13.     <Rule.Categories>  
  14.       <Category  
  15.         Name="General">  
  16.         <Category.DisplayName>  
  17.           <sys:String>常规</sys:String>  
  18.         </Category.DisplayName>  
  19.       </Category>  
  20.       <Category  
  21.         Name="Preprocessor">  
  22.         <Category.DisplayName>  
  23.           <sys:String>预处理器</sys:String>  
  24.         </Category.DisplayName>  
  25.       </Category>  
  26.       <Category  
  27.         Name="Assembler Options">  
  28.         <Category.DisplayName>  
  29.           <sys:String>汇编</sys:String>  
  30.         </Category.DisplayName>  
  31.       </Category>  
  32.       <Category  
  33.         Name="Advanced">  
  34.         <Category.DisplayName>  
  35.           <sys:String>高级 </sys:String>  
  36.         </Category.DisplayName>  
  37.       </Category>        
  38.       <Category  
  39.         Name="Command Line"  
  40.         Subtype="CommandLine">  
  41.         <Category.DisplayName>  
  42.           <sys:String>命令行</sys:String>  
  43.         </Category.DisplayName>  
  44.       </Category>  
  45.     </Rule.Categories>  
  46.     <StringProperty  
  47.       Name="Inputs"  
  48.       Category="Command Line"  
  49.       IsRequired="true">  
  50.       <StringProperty.DataSource>  
  51.         <DataSource  
  52.           Persistence="ProjectFile"  
  53.           ItemType="NASM"  
  54.           SourceType="Item" />  
  55.       </StringProperty.DataSource>  
  56.     </StringProperty>  
  57.       
  58.   <StringProperty  
  59.       Name="OutputFormat"        
  60.       Category="Assembler Options"  
  61.       HelpUrl="http://www.nasm.us/doc/"  
  62.       DisplayName="输出文件名"  
  63.       Description="指定输出目标文件名.-o [value]"  
  64.       Switch="-o [value]"  
  65.     />    
  66.   
  67.     <EnumProperty  
  68.       Name="Outputswitch"  
  69.       Category="Assembler Options"  
  70.       HelpUrl="http://www.nasm.us/doc/"  
  71.       DisplayName="目标格式"  
  72.       Description="选择一个平台相关的输出格式类型.如Windows选择win32或者win64;Linux选择ELF32或者ELF64。Windows链接器不能使用ELF和Binary格式,否则会出错">  
  73.       <EnumValue  
  74.         Name="0"  
  75.         DisplayName="Win32 微软Win32(x86)目标文件格式"  
  76.         Switch="-f win32" />  
  77.       <EnumValue  
  78.         Name="1"  
  79.         DisplayName="Win64 微软Win64(x64)目标文件格式"  
  80.         Switch="-f win64" />  
  81.       <EnumValue  
  82.         Name="2"  
  83.         DisplayName="COFF文件格式"  
  84.         Switch="-f coff" />   
  85.       <EnumValue  
  86.         Name="3"  
  87.         DisplayName="Linux x86 ELF格式"  
  88.         Switch="-f elf32" />   
  89.       <EnumValue  
  90.         Name="4"  
  91.         DisplayName="Linux x64 ELF格式"  
  92.         Switch="-f elf64" />   
  93.       <EnumValue  
  94.         Name="5"  
  95.         DisplayName="纯二进制文件格式"  
  96.         Switch="-f bin" />   
  97.     </EnumProperty>      
  98.       
  99.     <StringListProperty  
  100.     Name="AssembledCodeListingFile"  
  101.     Category="Assembler Options"  
  102.     DisplayName="汇编代码列表文件"      
  103.     Description="生成一个汇编代码列表文件.     (-l [file])"  
  104.     HelpUrl="http://www.nasm.us/doc/"  
  105.     Switch="-l &quot;[value]&quot;"  
  106.     />  
  107.       
  108.     <BoolProperty  
  109.     Name="GenerateDebugInformation"  
  110.     Category="Assembler Options"  
  111.     DisplayName="生成调试信息"  
  112.     Description="生成调试信息.     (-g)"  
  113.     HelpUrl="http://www.nasm.us/doc/"  
  114.     Switch="-g"      
  115.     />  
  116.         
  117.   <StringListProperty  
  118.       Name="ErrorReporting"  
  119.       Category="Advanced"  
  120.       HelpUrl="http://www.nasm.us/doc/"  
  121.       DisplayName="重定向错误信息到文件"  
  122.       Description="将错误信息输出到文件"  
  123.       Switch="-Z &quot;[value]&quot;"          
  124.     />  
  125.       
  126.     <StringListProperty  
  127.     Name="IncludePaths"  
  128.     Category="General"  
  129.     DisplayName="包含目录"  
  130.     Description="设置包含文件路径.     (-I[path])"  
  131.     HelpUrl="http://www.nasm.us/doc/"  
  132.     Switch="-I[value]"  
  133.     />  
  134.       
  135.     <StringListProperty  
  136.     Name="PreprocessorDefinitions"  
  137.     Category="Preprocessor"  
  138.     HelpUrl="http://www.nasm.us/doc/"  
  139.     DisplayName="预处理器定义"  
  140.     Description="预处理器定义.     (-D[symbol])"  
  141.     Switch="-D[value]"  
  142.     />  
  143.       
  144.   <StringListProperty  
  145.     Name="UndefinePreprocessorDefinitions"  
  146.     Category="Preprocessor"  
  147.     HelpUrl="http://www.nasm.us/doc/"  
  148.     DisplayName="取消预处理器定义"  
  149.     Description="取消预处理器定义.     (-U[symbol])"      
  150.     Switch="-U[value]"  
  151.     />  
  152.       
  153.     <EnumProperty  
  154.       Name="ErrorReportingFormat"  
  155.       Category="Advanced"  
  156.       HelpUrl="http://www.nasm.us/doc/"  
  157.       DisplayName="错误报告格式"  
  158.       Description="选择一个错误报告的格式,如GNU或者VC">  
  159.       <EnumValue  
  160.         Name="0"  
  161.         DisplayName="-Xgnu    GNU风格(默认)"  
  162.         Switch="-Xgnu" />  
  163.       <EnumValue  
  164.         Name="1"  
  165.         DisplayName="-Xvc Microsoft Visual C++风格"  
  166.         Switch="-Xvc" />        
  167.     </EnumProperty>  
  168.       
  169.     <BoolProperty  
  170.     Name="TreatWarningsAsErrors"  
  171.     Category="Assembler Options"  
  172.     DisplayName="将警告视为错误"  
  173.     Description="将所有编译器警告都视为错误.     (-Werror)"  
  174.     HelpUrl="http://www.nasm.us/doc/"  
  175.     Switch="-Werror"  
  176.     />  
  177.   
  178.     <BoolProperty  
  179.       Name="floatunderflow"  
  180.       Category="Advanced"  
  181.       HelpUrl="http://www.nasm.us/doc/"  
  182.       DisplayName="float-underflow"  
  183.       Description="floating point underflow (default off)"  
  184.       Switch="-w+float-underflow" />  
  185.   
  186.   <BoolProperty  
  187.       Name="macrodefaults"  
  188.       Category="Advanced"  
  189.       HelpUrl="http://www.nasm.us/doc/"  
  190.       DisplayName="Disable macro-defaults"  
  191.       Description="macros with more default than optional parameters (default on)"  
  192.       Switch="-w-macro-defaults" />  
  193.   
  194.   <BoolProperty  
  195.       Name="user"  
  196.       Category="Advanced"  
  197.       HelpUrl="http://www.nasm.us/doc/"  
  198.       DisplayName="Disable user"  
  199.       Description="%warning directives (default on)"  
  200.       Switch="-w-user" />  
  201.   
  202.   <BoolProperty  
  203.       Name="floatoverflow"  
  204.       Category="Advanced"  
  205.       HelpUrl="http://www.nasm.us/doc/"  
  206.       DisplayName="Disable float-overflow"  
  207.       Description="floating point overflow (default on)"  
  208.       Switch="-w-float-overflow" />  
  209.   
  210.   <BoolProperty  
  211.       Name="floatdenorm"  
  212.       Category="Advanced"  
  213.       HelpUrl="http://www.nasm.us/doc/"  
  214.       DisplayName="float-denorm"  
  215.       Description="floating point denormal (default off)"  
  216.       Switch="-w+float-denorm" />  
  217.   
  218.   <BoolProperty  
  219.       Name="numberoverflow"  
  220.       Category="Advanced"  
  221.       HelpUrl="http://www.nasm.us/doc/"  
  222.       DisplayName="Disable number-overflow"  
  223.       Description="numeric constant does not fit (default on)"  
  224.       Switch="-w-number-overflow" />  
  225.   
  226.   <BoolProperty  
  227.       Name="macroselfref"  
  228.       Category="Advanced"  
  229.       HelpUrl="http://www.nasm.us/doc/"  
  230.       DisplayName="macro-selfref"  
  231.       Description="cyclic macro references (default off)"  
  232.       Switch="-w+macro-selfref" />  
  233.   
  234.   <BoolProperty  
  235.       Name="floattoolong"  
  236.       Category="Advanced"  
  237.       HelpUrl="http://www.nasm.us/doc/"  
  238.       DisplayName="Disable float-toolong"  
  239.       Description=" too many digits in floating-point number (default on)"  
  240.       Switch="-w-float-toolong" />  
  241.   
  242.   <BoolProperty  
  243.       Name="orphanlabels"  
  244.       Category="Advanced"  
  245.       HelpUrl="http://www.nasm.us/doc/"  
  246.       DisplayName="Disable orphan-labels"  
  247.       Description="labels alone on lines without trailing `:' (default on)"  
  248.       Switch="-w-orphan-labels" />  
  249.   
  250.   <StringProperty  
  251.       Name="CommandLineTemplate"  
  252.       DisplayName="命令行"  
  253.       Visible="False"  
  254.       IncludeInCommandLine="False" />  
  255.   
  256.   <DynamicEnumProperty  
  257.         Name="NASMBeforeTargets"  
  258.         Category="General"  
  259.         EnumProvider="Targets"  
  260.         IncludeInCommandLine="False">  
  261.       <DynamicEnumProperty.DisplayName>  
  262.         <sys:String>Execute Before</sys:String>  
  263.       </DynamicEnumProperty.DisplayName>  
  264.       <DynamicEnumProperty.Description>  
  265.         <sys:String>Specifies the targets for the build customization to run before.</sys:String>  
  266.       </DynamicEnumProperty.Description>  
  267.       <DynamicEnumProperty.ProviderSettings>  
  268.         <NameValuePair  
  269.           Name="Exclude"  
  270.           Value="^NASMBeforeTargets|^Compute" />  
  271.       </DynamicEnumProperty.ProviderSettings>  
  272.       <DynamicEnumProperty.DataSource>  
  273.         <DataSource  
  274.           Persistence="ProjectFile"  
  275.           ItemType=""  
  276.           HasConfigurationCondition="true" />  
  277.       </DynamicEnumProperty.DataSource>  
  278.     </DynamicEnumProperty>  
  279.   <DynamicEnumProperty  
  280.       Name="NASMAfterTargets"  
  281.       Category="General"  
  282.       EnumProvider="Targets"  
  283.       IncludeInCommandLine="False">  
  284.       <DynamicEnumProperty.DisplayName>  
  285.         <sys:String>Execute After</sys:String>  
  286.       </DynamicEnumProperty.DisplayName>  
  287.       <DynamicEnumProperty.Description>  
  288.         <sys:String>Specifies the targets for the build customization to run after.</sys:String>  
  289.       </DynamicEnumProperty.Description>  
  290.       <DynamicEnumProperty.ProviderSettings>  
  291.         <NameValuePair  
  292.           Name="Exclude"  
  293.           Value="^NASMAfterTargets|^Compute" />  
  294.       </DynamicEnumProperty.ProviderSettings>  
  295.       <DynamicEnumProperty.DataSource>  
  296.         <DataSource  
  297.           Persistence="ProjectFile"  
  298.           ItemType=""  
  299.           HasConfigurationCondition="true" />  
  300.       </DynamicEnumProperty.DataSource>  
  301.     </DynamicEnumProperty>  
  302.   <StringProperty  
  303.       Name="ExecutionDescription"  
  304.       DisplayName="Execution Description"  
  305.       IncludeInCommandLine="False"  
  306.       Visible="False" />  
  307.   
  308.   <StringListProperty  
  309.       Name="AdditionalDependencies"  
  310.       DisplayName="Additional Dependencies"  
  311.       IncludeInCommandLine="False"  
  312.       Visible="False" />  
  313.     
  314.   <StringProperty  
  315.       Subtype="AdditionalOptions"  
  316.       Name="附加选项"  
  317.       Category="Command Line">  
  318.       <StringProperty.DisplayName>  
  319.         <sys:String>附加选项</sys:String>  
  320.       </StringProperty.DisplayName>  
  321.       <StringProperty.Description>  
  322.         <sys:String>附加选项</sys:String>  
  323.       </StringProperty.Description>  
  324.     </StringProperty>  
  325.     
  326.   </Rule>  
  327.   <ItemType  
  328.     Name="NASM"  
  329.     DisplayName="Netwide Assembler" />  
  330.   <FileExtension  
  331.     Name="*.asm"  
  332.     ContentType="NASM" />  
  333.   <ContentType  
  334.     Name="NASM"  
  335.     DisplayName="Netwide Assembler"  
  336.     ItemType="NASM" />  
  337. </ProjectSchemaDefinitions>  
1 0
原创粉丝点击