MSBuild试图构建exe而不是dll

5

我手动创建了一个.csproj文件,以便使用命令行工具msbuild运行,但当我尝试运行它时,它想要生成exe。如何使它专门生成dll?以下是.csproj文件中的代码和我执行的命令提示符:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Data.Linq" />
    </ItemGroup>
    <ItemGroup>
        <Compile Include="C:\testing\test.cs" />
        <Compile Include="C:\testing\test.Designer.cs" />
        <EmbeddedResource Include="C:\testing\test.resx" />
    </ItemGroup>
    <Target Name="Build">
        <Csc Sources="@(Compile)" 
             Resources="@(EmbeddedResource)" 
             References="@(Reference)" 
             TargetType="library"
             OutputAssembly="C:\testing\test.dll" />
    </Target>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

cmd: msbuild "C:\testing\test.csproj"

虽然我不能确定,但我相信去掉TargetType应该可以解决问题。根据文档, 默认的OutputTypeLibrary - Mike Perrenoud
你的 <Target Name="Build"> 被调用了吗?它不会被 Import 覆盖吗? - rene
@rene 老实说......我不能确定,因为我今天才开始研究它。当我没有进行导入时,它无法编译。 - 5tar-Kaster
2
目前生成msbuild脚本的最佳方式是使用Visual Studio IDE。它生成的.csproj文件已经很好了。您将轻松避免错误,例如将<Import>标签放在错误的位置以替换您的<Csc>目标。 - Hans Passant
@HansPassant 很遗憾,在我的情况下这不是一个选项,我很乐意让VS来处理建筑物。 - 5tar-Kaster
不是用于构建,而是用于创建文件。Express 版本是免费的。 - Hans Passant
4个回答

7
请在您的csproj文件中添加以下内容:
<PropertyGroup>
  <OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>声明之上。

有趣。这似乎在使用mono系统上的xbuild时无法工作。 - weberc2

0

您需要将以下内容添加到您的项目文件中:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
  <OutputType>Library</OutputType>
  <!-- Other properties go here -->
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
  <OutputType>Library</OutputType>
  <!-- Other properties go here -->
</PropertyGroup>

你可能有更多的平台,只需添加适合您需求的库。

或者您可以简单地执行以下操作:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
 <OutputType>Library</OutputType>
      <!-- Other properties go here -->
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
 <OutputType>Library</OutputType>
      <!-- Other properties go here -->
</PropertyGroup>

这能够工作是因为使用了导入的构建目标,对吧? - rene

0
一旦您导入Microsoft.CSharp.targets文件,就可以获取与其相关的所有生态系统构建目标。
下面的示例可以工作。请注意OutputType属性。
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <OutputType>Library</OutputType>    
    </PropertyGroup>
    <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Data.Linq" />
    </ItemGroup>
    <ItemGroup>
        <Compile Include="Class1.cs" />
    </ItemGroup>
    <!-- You don't need to call the Csc target as Build target is already there once you import the Microsoft.CSharp.targets file -->
    <!--<Target Name="Build">
        <Csc Sources="@(Compile)" 
             Resources="@(EmbeddedResource)" 
             References="@(Reference)" 
             TargetType="library"
             OutputAssembly="abc.dll" />
    </Target>-->
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0

我想我找到了答案... 你必须在你的.csproj文件中包含这两行代码

<Project>
<PropertyGroup>
    <!-- Add Output Type -->
    <OutputType>Library</OutputType>
</PropertyGroup>
<!-- Add the MSBuildTools Targets as a reference -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

需要注意的一点:一个项目文件只能创建WinExe类型或Library类型...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接