简体   繁体   English

在Visual Studio中使用外部工具进行C#构建

[英]Using an external tool for C# builds in Visual Studio

When using Visual Stdio 2008, you can make a C++ project build with an internal tool rather than having the IDE invoke MSVC directly. 使用Visual Stdio 2008时,可以使用内部工具构建C ++项目,而不是让IDE直接调用MSVC。 This improves the consistency of builds across platforms if a cross-platform build system is used. 如果使用跨平台构建系统,这可以提高跨平台构建的一致性。

However, I cannot figure out how to do the same as a C# project. 但是,我无法弄清楚如何像C#项目那样做。 It would be possible to simply register it as a native project with C# sources, however, you lose some of the advantages gained through having a C# project. 可以简单地将其注册为具有C#源的本机项目,但是,您将失去通过C#项目获得的一些优势。 More importantly, it will mean that allowing a project to build both directly and with an external tool (which is sadly necessary) will require two separate projects, rather than merely creating an alternate build configuration to invoke the external tool. 更重要的是,这意味着允许项目直接构建和使用外部工具(这是非常必要的)将需要两个单独的项目,而不是仅仅创建一个备用构建配置来调用外部工具。

Does anyone know if it's possible to prevent Visual Studio from invoking csc by itself and instead call an external tool? 有谁知道是否可以阻止Visual Studio自己调用csc而是调用外部工具?

EDIT: Apparently there has some misunderstanding. 编辑:显然有一些误解。 The goal here is not to compile anything outside of Visual Studio. 这里的目标不是编译Visual Studio之外的任何东西。 Instead, it's to allow Visual Studio to serve as the IDE but not the build system. 相反,它允许Visual Studio作为IDE而不是构建系统。 There is already a (Scons-based) build system capable of compiling the C# and C++ sources, and Visual Studio has been configured to invoke Scons for compilation of C++ projects. 已经有一个(基于Scons的)构建系统能够编译C#和C ++源代码,并且Visual Studio已经配置为调用Scons来编译C ++项目。 I'm trying to configure it so that when you hit the 'Build' button, it will invoke Scons for the C# projects as well as the C++ ones. 我正在尝试对其进行配置,以便当您点击“构建”按钮时,它将为C#项目以及C ++项目调用Scons。

Edit: Your question is still answered using MSBuild(if you are simply looking to compile outside the IDE). 编辑:您仍然可以使用MSBuild回答您的问题(如果您只是想在IDE外部编译)。 The IDE(Visual Studios) is simply a "fancy" way of constructing the build files that are built by MSBuild. IDE(Visual Studios)只是构建由MSBuild构建的构建文件的“奇特”方式。 Visual Studios isn't building the files, it simply is invoking MSBuild which ships with the .NET Framework 2.0 and up which compiles your code based on the project file that you create. Visual Studios不构建文件,它只是调用随.NET Framework 2.0一起提供的MSBuild,它根据您创建的项目文件编译代码。 If Scons can read and process an MSBuild file then I'm sure you can invoke it to build your project. 如果Scons可以读取并处理MSBuild文件,那么我相信你可以调用它来构建你的项目。 But considering the fact that C# is a Microsoft language, I think you will be hard-pressed to find a value-add in not using MSBuild since I'd assume both the language and build tool are very tuned to work together. 但考虑到C#是Microsoft语言这一事实​​,我认为你很难找到不使用MSBuild的增值,因为我认为语言和构建工具都非常适合一起工作。 - End Edit - 结束编辑

You can use MSBuild to compile your C# project. 您可以使用MSBuild编译C#项目。 If you open your .csproj file in a text editor you will see that it is a MSBuild file. 如果在文本编辑器中打开.csproj文件,您将看到它是MSBuild文件。 If you want to write some C# outside of the IDE you can construct a build file using the .csproj file as a starting point and invoke MSBuild to compile your apps. 如果您想在IDE之外编写一些C#,您可以使用.csproj文件作为起点构建构建文件,并调用MSBuild来编译您的应用程序。 The IDE is just a way of abstracting the editing of the MSBuild file away for you. IDE只是一种抽象编辑MSBuild文件的方法。

If you are really industrious you can create a set of custom tasks to do things in your custom build process like move files around and versioning. 如果您真的很勤奋,您可以创建一组自定义任务,以便在自定义构建过程中执行操作,例如移动文件和版本控制。 MSBuild Community Tasks are a great example of using custom code to do task for you during MSBuild. MSBuild社区任务是在MSBuild期间使用自定义代码为您执行任务的一个很好的示例。

Given all the other answers, what MSBuild does when either VS or MSBuild perform a build can be found in the Targets files that ship with .Net. 鉴于所有其他答案,MSBuild在VS或MSBuild执行构建时所执行的操作可以在.Net附带的目标文件中找到。 These can be be found in the FrameWork directory on your system. 这些可以在系统的FrameWork目录中找到。 In my case: 就我而言:

C:\Windows\Microsoft.NET\Framework64\v3.5

Contains Microsoft.Common.targets among others. 包含Microsoft.Common.targets等。 This file contains the following snippit: 该文件包含以下snippit:

<!--
============================================================
                                    Build

The main build entry point.
============================================================
-->
<PropertyGroup>
    <BuildDependsOn>
        BeforeBuild;
        CoreBuild;
        AfterBuild
    </BuildDependsOn>
</PropertyGroup>
<Target
    Name="Build"
    Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
    DependsOnTargets="$(BuildDependsOn)"
    Outputs="$(TargetPath)"/>

This means that redifining this Target you can make MSBuild an VS do anything you want. 这意味着重新定义此Target可以使MSBuild和VS做任何你想做的事情。 The top of the mentioned file contains an important messagge: 上述文件的顶部包含一个重要的信息:

Microsoft.Common.targets

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

This file defines the steps in the standard build process for .NET projects.  It
contains all the steps that are common among the different .NET languages, such as
Visual Basic, C#, and Visual J#.

My suggestion would be to read all you can about MSBuild and it's build file syntax and try redifining the Build target in your project(s). 我的建议是阅读有关MSBuild及其构建文件语法的所有内容,并尝试在项目中重新构建Build目标。 My impression is that after reading up on MSBuild you'll probably find an easier way to meet your requierements. 我的印象是,在阅读完MSBuild后,您可能会找到一种更简单的方式来满足您的需求。 You can find an example of redifining a Target like this in one of the answers of this so question . 你可以在这个问题的答案中找到一个像这样重新定义目标的例子。

Edit: 编辑:

How to redefine a target? 如何重新定义目标? Redefining is essentially defining the same target 'after' it has been defined. 重新定义基本上是在定义之后定义相同的目标。 So for instance in your .*proj file(s) define a Build Task after the <Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" /> line that imports all targets needed to in this case build a C# project. 因此,例如在。* proj文件中,在<Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" />行之后定义Build Task ,该行导入在这种情况下构建C#项目所需的所有目标。 An example could be 一个例子可能是

<Target
    Name="Build"
    Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
    DependsOnTargets="BeforeBuild"
    Outputs="$(TargetPath)">
    <Exec Command="nmake" />
</Target>

I found a question in the same direction here , where it is suggested to edit the registry. 我在这里找到了一个相同方向的问题,建议编辑注册表。 I am pretty sure there is no other way to change the compiler used by Visual Studio because there is no trace of csc.exe in any solution, config, csproj file or whatsoever, nor in the Visual Studio 9.0 folder / subfolders within the Program Files dir. 我很确定没有其他方法可以更改Visual Studio使用的编译器,因为在任何解决方案,配置,csproj文件或任何内容中都没有csc.exe的痕迹,也没有在Program Files中的Visual Studio 9.0文件夹/子文件夹中DIR。

Registry locations can be found in: 注册表位置可在以下位置找到:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\74ACAA9F1F0087E4882A06A5E18D7D32
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\9055DA7481CC1024CB23A6109FD8FC9B

but those keys may differ dependng on your installation. 但这些键可能因您的安装而异。 Conclusion: changing the compiler used by VS seems next to impossible. 结论:改变VS使用的编译器几乎是不可能的。

Addition: The following MSDN article deals with the same question for an custom C++ compiler, and Ed Dore's answer seems to confirm my theory that there's no way to choose an custom compiler for use within VS. 另外:以下MSDN文章针对自定义C ++编译器处理相同的问题,Ed Dore的回答似乎证实了我的理论,即无法选择在VS中使用的自定义编译器。

Under 'Tools' > 'External Tools' you should be able to define an outside tool to do activities for you. 在“工具”>“外部工具”下,您应该能够定义一个外部工具来为您执行活动。 The Command should be the path to the executible for your external tool. Command应该是外部工具的可执行文件的路径。

Hope this helps some. 希望这会有所帮助。

You don't have to maintain different project files to build using an external tool. 您不必使用外部工具维护要构建的不同项目文件。 MSBuild is designed to build using the same project files that Visual Studio uses. MSBuild旨在使用Visual Studio使用的相同项目文件进行构建。

Here's an article that describes it. 这是一篇描述它的文章。

Customize Your Builds in Visual Studio Using the Standalone MSBuild Tool 使用独立的MSBuild工具在Visual Studio中自定义构建

It's for VS2005, but should apply to VS2008 as well. 它适用于VS2005,但也适用于VS2008。

您可以从命令行构建解决方案,如下所示:

C:\WINDOWS\Microsoft.NET\Framework\v3.5>msbuild.exe "C:\path\Your Solution.sln"

Edit your project file and update the CscToolPath keys to point to the directory containing your tool and add CscToolExe keys that holds the name of the directory: 编辑项目文件并更新CscToolPath键以指向包含工具的目录,并添加包含目录名称的CscToolExe键:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|.NET 3.5' ">
   .
   .
   .
   <CscToolPath>path\to\custom\tool\directory</CscToolPath>
   <CscToolExe>exe name</CscToolExe>
   .
   .
   .
</PropertyGroup>

I have not tested this, and the CscToolExe key may cause problems, in which case I would simply rename the external tool executable to "csc.exe". 我没有测试过,CscToolExe键可能会导致问题,在这种情况下,我只需将外部工具可执行文件重命名为“csc.exe”。

Looking through the answers, it seems clear to me that integrating scons into Visual Studio in a way that is compatible with the debugger and so on is not going to happen... 通过回答,我似乎很清楚,以与调试器兼容的方式将scons集成到Visual Studio中是不会发生的......

An option you might to consider, and I understand you don't want to change build systems, but bear with me, is to use a meta-build system, ie 'cmake'. 您可能要考虑的一个选项,我知道您不想更改构建系统,但请耐心等待使用元构建系统,即'cmake'。 http://www.cmake.org/ http://www.cmake.org/

Cmake doeesn't actually build the project. Cmake doees实际上并没有建立这个项目。 What it does is to create build files for you, that you can use to build the project, and on Windows, the build files it creates for you are: Visual Studio project files. 它的作用是为您创建构建文件,您可以使用它来构建项目,在Windows上,它为您创建的构建文件是:Visual Studio项目文件。 You can simply load those directly into your IDE, and compile, and use normally! 您可以直接将它们加载到IDE中,然后进行编译,并正常使用!

CMake is I feel very easy to use, and provides a high level of transparence and maintainability. CMake让我觉得非常容易使用,并提供高水平的透明度和可维护性。

The exact same CMakeLists.txt files on linux will causes linux makefiles to be generated. linux上完全相同的CMakeLists.txt文件将导致生成linux makefile。

On mingw, they can generate mingw makefiles. 在mingw,他们可以生成mingw makefile。

There are numerous generators available within cmake. cmake中有许多发生器。 The list is here: 清单在这里:

http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Generators http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Generators

http://springrts.com is a huge opensource rts game that used to use scons as its cross-platform build system and now uses cmake. http://springrts.com是一个巨大的开源rts游戏,曾经使用scons作为其跨平台构建系统,现在使用cmake。

I understand that you don't really want to have to change build systems, so it is a medium to long term solution. 我知道你真的不想改变构建系统,因此它是一个中长期解决方案。

Cmake is in any case one more option, to add to those of using a custom build tool, or using msbuild, or running the scons build from the commandline by hand. 在任何情况下,Cmake都是一个选项,可以添加到使用自定义构建工具,或使用msbuild,或者从命令行手动运行scons构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自从切换到VS 2015并使用C#6 Roslyn功能以来,Visual Studio Online CI的每日构建失败 - Visual Studio Online CI daily builds fail since switching to VS 2015 and using C# 6 Roslyn features Visual Studio 2013仅生成HTML更改,而不生成C#更改 - Visual Studio 2013 only builds HTML changes, not C# changes 在Visual Studio C#中控制外部窗口 - Control external window in visual studio c# 外部DLL Visual Studio C#错误 - external dll visual studio C# error 根据窗口,工具条不会拉伸和自动装配! (在c#中,使用Visual Studio 2010) - Tool Strip wont stretch and autofit, according to the window! (in c#, using Visual Studio 2010) 使用 MS Visual Studio xsd 工具为 gml.xsd 生成 C# 类 - Generating C# classes for gml.xsd using MS Visual studio xsd tool 单击Visual Studio C#更改按钮工具提示 - visual studio c# change button tool tip on click C#Visual Studio-与格式化文本一起使用的更好工具 - c# visual studio - Better tool to use with formatted text Visual Studio 2012 C#的内存诊断工具 - memory diagnostic tool for visual studio 2012 C# 使用C#在Visual Studio 2010中更改或向外部.dll添加路径 - Change or add a path to external .dll in Visual Studio 2010 using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM