visual-studio 如何从源代码编译 C# 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/858171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:29:03  来源:igfitidea点击:

How to Compile a C# Project from Source

visual-studiocompiler-construction

提问by Mehrdad Afshari

So I mean compile without visual studio

所以我的意思是在没有 Visual Studio 的情况下编译

回答by Mitch Wheat

From here:

这里

Compiles File.cs producing File.exe:

编译 File.cs 生成 File.exe:

csc File.cs

Compiles File.cs producing File.dll:

编译 File.cs 生成 File.dll:

csc /target:library File.cs

Compiles File.cs and creates My.exe:

编译 File.cs 并创建 My.exe:

csc /out:My.exe File.cs

Compiles all the C# files in the current directory, with optimizations on and defines the DEBUG symbol. The output is File2.exe:

编译当前目录中的所有 C# 文件,优化并定义 DEBUG 符号。输出是 File2.exe:

csc /define:DEBUG /optimize /out:File2.exe *.cs

回答by Mehrdad Afshari

Go to the project directory (I assume .NET framework is in your PATH):

转到项目目录(我假设 .NET 框架在您的 中PATH):

msbuild <enter>

If you want to compile a bunch of C# source files (not in a project), you'd use the csccommand. vbcis VB.NET compiler. jscis JScript.NET compiler. clis C++/CLI (& plain C++) compiler.

如果要编译一堆 C# 源文件(不在项目中),可以使用该csc命令。vbc是 VB.NET 编译器。jsc是 JScript.NET 编译器。cl是 C++/CLI(和普通 C++)编译器。

回答by Mehrdad Afshari

If you already have a solution or project file, use msbuildtool. You can find it deeply inside folder "%windir%\Microsoft.NET\". For example, on my machine I run the following to compile my project:

如果您已有解决方案或项目文件,请使用msbuild工具。您可以在文件夹“%windir%\Microsoft.NET\”的深处找到它。例如,在我的机器上,我运行以下命令来编译我的项目:

C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe MassiveMultithreading.sln

回答by CodeLikeBeaker

I created a batch file that i use for this, so I can compile multiple projects in a row. This should help you get in the right direction:

我创建了一个用于此的批处理文件,因此我可以连续编译多个项目。这应该可以帮助您找到正确的方向:

cls
echo off
c:


cd windows
cd microsoft.net
cd framework
cd v3.5

msbuild c:\Project\Project.sln /t:rebuild /p:Configuration=Debug /p:Platform="any cpu" /clp:Nosummary 

回答by Mike Caron

There are currently three ways to compile a C# project. The first is with Visual Studio, but you've stated that VS not installed is a constraint. The second is using the raw .NET SDK tools. The third way is to use Mono.

目前有三种方法可以编译 C# 项目。第一个是使用 Visual Studio,但您已经声明未安装 VS 是一个限制条件。第二种是使用原始 .NET SDK 工具。第三种方法是使用Mono

Using msbuild requires Visual Studio to be installed. Using csc does NOT, however it does require the .NET SDK to be installed.

使用 msbuild 需要安装 Visual Studio。使用 csc 不会,但它确实需要安装 .NET SDK。

Using .NET:

使用.NET:

  • Compiles File.cs producing File.exe:

    csc File.cs

  • Compiles File.cs producing File.dll:

    csc /target:library File.cs

  • Compiles File.cs and creates My.exe:

    csc /out:My.exe File.cs

  • Compiles all of the C# files in the current directory, with optimizations on and defines the DEBUG symbol. The output is File2.exe:

    csc /define:DEBUG /optimize /out:File2.exe *.cs

  • 编译 File.cs 生成 File.exe:

    csc File.cs

  • 编译 File.cs 生成 File.dll:

    csc /target:library File.cs

  • 编译 File.cs 并创建 My.exe:

    csc /out:My.exe File.cs

  • 编译当前目录中的所有 C# 文件,优化并定义 DEBUG 符号。输出是 File2.exe:

    csc /define:DEBUG /optimize /out:File2.exe *.cs

Using Mono:

使用单声道:

  • Compiles File.cs producing File.exe compat with .NET 1.1:

    mcs File.cs

  • 编译 File.cs 生成 File.exe 与 .NET 1.1 兼容:

    mcs File.cs

or for .NET 2.0 compatible

或 .NET 2.0 兼容

gmcs File.cs
  • Compiles File.cs producing File.dll:

    msc /target:library File.cs

  • Compiles File.cs and creates My.exe:

    msc /out:My.exe File.cs

  • Compiles all of the C# files in the current directory, with optimizations on and defines the DEBUG symbol. The output is File2.exe:

    msc /define:DEBUG /optimize /out:File2.exe *.cs

  • 编译 File.cs 生成 File.dll:

    msc /target:library File.cs

  • 编译 File.cs 并创建 My.exe:

    msc /out:My.exe File.cs

  • 编译当前目录中的所有 C# 文件,优化并定义 DEBUG 符号。输出是 File2.exe:

    msc /define:DEBUG /optimize /out:File2.exe *.cs

回答by Hannoun Yassir

use command line with csc.exeor msbuild

将命令行与csc.exemsbuild 一起使用

回答by Serapth

Set your path or change directory to C:\Windows\Microsoft.NET\Framework\v2.0.50727 ( note, if directory if using different framework version ).

设置您的路径或将目录更改为 C:\Windows\Microsoft.NET\Framework\v2.0.50727 (注意,如果使用不同框架版本的目录)。

type:

类型:

MSBuild /path/to/your/project/projectname.solution /rebuild

MSBuild /path/to/your/project/projectname.solution /rebuild

Or, using csc from the commandline. Again, switch to the directory mentioned above, this time the command is

或者,从命令行使用 csc。再次切换到上面提到的目录,这次的命令是

csc /out:filename.exe /path/to/your/project/*.cs

csc /out:filename.exe /path/to/your/project/*.cs

Note, CSC.exe has a few hundred operations. Type csc -help for more details.

请注意,CSC.exe 有几百个操作。键入 csc -help 以获取更多详细信息。