编写 DLL 生成的脚本
本文关键字:脚本 DLL 编写 | 更新日期: 2023-09-27 18:31:04
我想构建一个具有常用函数的库。因此,我创建了一个Microsoft Visual C# 2010 项目。此项目包含多个*.cs
文件,每个文件包含一个类。目前我的项目包含两个文件Common.cs
和Vars.cs
。为了从我的项目中构建 dll,我在命令行中运行以下命令。
C:'Windows'Microsoft.NET'Framework'v2.0.50727'csc /target:library /reference:some.dll /out:Library.dll Library'Common.cs Library'Vars.cs
我的问题是,如果我添加其他*.cs
文件,我必须编辑此构建命令。
如何创建构建脚本来自动执行此过程?
编辑:这是对我有用的解决方案:
我创建了一个名为 build.xml
的文件。这是此文件的内容。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CompileAll" ToolsVersion="3.5">
<Import Project="$(MSBuildToolsPath)'Microsoft.CSharp.targets" />
<!-- Import Project="c:'.net3.5'Microsoft.Csharp.targets" / -->
<PropertyGroup>
<!-- This AppName thing is the base name of your DLL or EXE -->
<AppName>MyLibraryName</AppName>
</PropertyGroup>
<!-- This build file compiles each .cs file into its own exe -->
<PropertyGroup Condition="'$(Configuration)'==''">
<Configuration>Debug</Configuration>
<!-- Default -->
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<Optimize>false</Optimize>
<DebugSymbols>true</DebugSymbols><!-- <OutputPath>.'bin</OutputPath> -->
<OutputPath>.'</OutputPath>
<OutDir>.'</OutDir>
<IntermediateOutputPath>.'</IntermediateOutputPath>
</PropertyGroup>
<!-- Specify the inputs by type and file name -->
<ItemGroup>
<CSFile Include="LibraryEtasHil'*.cs"/>
</ItemGroup>
<!-- specify reference assemblies for all builds in this project -->
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Runtime.Serialization" />
<!-- <Reference Include=".'ObjectDumper.dll" /> -->
<Reference Include="dummy.dll" />
</ItemGroup>
<Target Name="CompileAll" DependsOnTargets="ResolveAssemblyReferences">
<Message Text="Reference = @(Reference)" />
<Message Text="ReferencePath = @(ReferencePath)" />
<!-- Message Text="MS Build Tools path: $(MSBuildToolsPath)" / -->
<!-- Run the Visual C# compilation on all the .cs files. -->
<CSC
Sources="@(CSFile)"
References="@(ReferencePath)"
OutputAssembly="$(OutputPath)'$(AppName).dll"
EmitDebugInformation="$(DebugSymbols)"
TargetType="library"
Toolpath="$(MSBuildToolsPath)"
Nologo="true"
/>
</Target>
<!-- redefine the Clean target, from the Microsoft.csharp.targets file. (Last definition wins) -->
<Target Name="Clean">
<Delete Files="$(OutputPath)'$(AppName).dll"/>
<Delete Files="$(OutputPath)'$(AppName).pdb"/>
<Delete Files="%(CSFile.identity)~"/>
<Delete Files="build.xml~"/>
</Target>
若要生成 dll,只需在 Visual Studio 命令提示符下执行以下命令。
msbuild build.xml
改为生成项目文件。项目文件实际上是一个 MSBuild 文件,因此只需从命令行生成项目文件即可获取输出。
此外,如果您了解项目文件的工作原理,则还可以学习 MSBuild,这是一种生成脚本语言,可用于以您喜欢的任何方式自定义生成。
开源替代品是Ant和Maven,但它的MSBuild可以很好地与Microsoft产品配合使用,因此更容易坚持使用MsBuild。
感谢您的建议。我能够为msbuild
创建一个有效的构建脚本。我创建了一个名为 build.xml
的文件。这是此文件的内容。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CompileAll" ToolsVersion="3.5">
<Import Project="$(MSBuildToolsPath)'Microsoft.CSharp.targets" />
<!-- Import Project="c:'.net3.5'Microsoft.Csharp.targets" / -->
<PropertyGroup>
<!-- This AppName thing is the base name of your DLL or EXE -->
<AppName>MyLibraryName</AppName>
</PropertyGroup>
<!-- This build file compiles each .cs file into its own exe -->
<PropertyGroup Condition="'$(Configuration)'==''">
<Configuration>Debug</Configuration>
<!-- Default -->
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<Optimize>false</Optimize>
<DebugSymbols>true</DebugSymbols><!-- <OutputPath>.'bin</OutputPath> -->
<OutputPath>.'</OutputPath>
<OutDir>.'</OutDir>
<IntermediateOutputPath>.'</IntermediateOutputPath>
</PropertyGroup>
<!-- Specify the inputs by type and file name -->
<ItemGroup>
<CSFile Include="LibraryEtasHil'*.cs"/>
</ItemGroup>
<!-- specify reference assemblies for all builds in this project -->
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Runtime.Serialization" />
<!-- <Reference Include=".'ObjectDumper.dll" /> -->
<Reference Include="dummy.dll" />
</ItemGroup>
<Target Name="CompileAll" DependsOnTargets="ResolveAssemblyReferences">
<Message Text="Reference = @(Reference)" />
<Message Text="ReferencePath = @(ReferencePath)" />
<!-- Message Text="MS Build Tools path: $(MSBuildToolsPath)" / -->
<!-- Run the Visual C# compilation on all the .cs files. -->
<CSC
Sources="@(CSFile)"
References="@(ReferencePath)"
OutputAssembly="$(OutputPath)'$(AppName).dll"
EmitDebugInformation="$(DebugSymbols)"
TargetType="library"
Toolpath="$(MSBuildToolsPath)"
Nologo="true"
/>
</Target>
<!-- redefine the Clean target, from the Microsoft.csharp.targets file. (Last definition wins) -->
<Target Name="Clean">
<Delete Files="$(OutputPath)'$(AppName).dll"/>
<Delete Files="$(OutputPath)'$(AppName).pdb"/>
<Delete Files="%(CSFile.identity)~"/>
<Delete Files="build.xml~"/>
</Target>
若要生成 dll,只需在 Visual Studio 命令提示符下执行以下命令。
msbuild build.xml