将NASM汇编器集成到Visual Studio中
之前在《 Visual Studio中使用NASM编译汇编文件》中介绍了如何将NASM汇编器集成到VS2005和VS2008中,但VS2010与VS2012的“生成自定义”与VS2005和VS2008的配置文件不一样了,需要重新进行配置。笔者在《 Integrating a compiler/assembler in VS ; Using NASM with Visual Studio 2010》一文中找到了相应的配置文件,并简单作了一点界面上的汉化(后面将附上其内容)。
在VS2010和VS2012中有三个配置文件控制自定义生成规则,分别是*.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作为其默认编译器,如下图所示:
至此,我们可以使用集成在VS中NASM编译器编译外联汇编了。但是有一个问题就是NASM默认的错误输出格式为GNU风格的,在VS中不能使用其直接定位到错误文件行。如下图:
为了使VS能直接提示错误并可定位到文件件行,需要设置为错误格式为VC风格:
这样,在编译错误时会提示如下:
至此,我们可以尽情的使用NASM编译外联汇编文件了,使用起来与C/C++一样的感觉,最后将三个配置文件附上:
nasm.props文件内容:
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>```
20
21
22
23 nasm.targets文件内容:
24
25
26
27
nasm.xml文件内容:
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 "[value]""
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 "[value]""
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>
- 原文作者:Witton
- 原文链接:https://wittonbell.github.io/posts/2013/2013-06-01-将NASM汇编器集成到Visual-Studio中/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。