What is the relationship between Visual Studio, MSBuild and CSC?
Asked Answered
L

1

6

I want to have an overall picture what is the relationship between Visual Studio, MSBuild and CSC?

I don't want to understand all of the aspects of each of them. But an overall picture is good.

Two important things are:

  • Responsibility of each of them
  • Relationship between each of them
Lavalava answered 9/7, 2019 at 7:38 Comment(8)
VS uses MSBuild, MSBuild uses CSC. However for "a deep understanding" a single question is way too broad.Disposition
I don't want an answer that covers all of the aspects of them. But an answer that shows a good overall picture.Lavalava
So you want a deep understanding or an overall picture? Seems like a contradiction to me.Disposition
I updated my question and with more explanation. Thank you.Lavalava
Even describing the "Responsibility of VS" allone would be far too much here, as you can do so many things with it. The same applies to the others. Not to mention how those are connected.Disposition
Not all responsibilities or relations. Just highlights, that can help you as a programmer to know what is the difference between msbuild and csharp compiler and how they are connected together. (Again: Not with all of the details)Lavalava
And what about dotnet CLI and nuget restore \ dotnet restore?Aryan
No. Just a very overall picture. Especially I don't know what is the role of msbuild.exeLavalava
Z
6

Responsibility of each of them:

CSC.exe is the C# compiler which can compile your C# code and produces executable (.exe) files, dynamic-link libraries (.dll), or code modules (.netmodule).

MSBuild is Microsoft Build Engine which can be used to build projects and solutions from Visual Studio. Besides, even if I wrote some .cs files and a custom project file (.xxproj) to structure them, we can use msbuild.exe to build them using a command like msbuild xxx.xxproj. See this document.

Visual Studio is a software suite that consolidates the basic tools required to write and test software.

Relationship between each of them:

  1. If I only have several .cs files and I want to compile them to output a .exe and what, then csc.exe is enough. Use it in command-line (by cmd.exe or other things) to compile the code.

  2. If I create a project file (.xxproj) to better control the resources, the .cs files, and other files, I can use msbuild in command-line to build them to output a .exe or what. And one point we should know is msbuild.exe can not only build C# code but also VB.net, C++, F#... When I use msbuild to build C# code using a command like msbuild xx.csproj, it will call csc.exe to compile C# code and pass parameters read from the .csproj file to it.

  3. And though msbuild is the build system in Visual Studio, it doesn't depend on Visual Studio. So you can install a separate Build Tools package for VS2019.

So I think it's clear that msbuild calls csc.exe when building C# projects. And Visual Studio uses MSBuild as its build engine, so it will always call msbuild when building projects and solutions in Visual Studio.

And since MSBuild is a separate package since VS2015(not sure about the time), we can also install a free Build Tools package which contains msbuild.exe to build projects and solutions outside VS IDE.

(From VS2017 and higher, for earlier VS versions like VS2010, VS2012, Visual Studio calls msbuild API to build projects instead of calling the msbuild.exe process)

In addition: You can get a picture here which describes the relationship for earlier VS versions. But I think the situation has changed since in earlier versions, I'm sure VS build call msbuild API instead of msbuild.exe, but for at least VS2017, when building C# project, it's obvious that it calls msbuild.exe as a separate process when you monitor the build process by task manager.

Hope it helps and if I misunderstand anything in what I wrote above, feel free to contact me to correct it :)

Zorina answered 9/7, 2019 at 9:39 Comment(3)
Can we say that Visual Studio beside all of the responsibilities creates .csproj file and then call and feed msbuild.exe with that file and then msbuild.exe call csc.exe with appropriate command parameters that extracted from the .csproj file?Lavalava
@KazemJavadi Not certainly sure about it. But i keep the same opinion like yours. Just for build process in VS, I think VS call the msbuild.exe and feed it with parameters like path/xx.csproj(project file), Debug (Configuration property), X86(Platform property), and then msbuild.exe will read content from xx.csproj and do what he should according to the rule(how to call csc.exe, how to copy assembly to output folder...).Zorina
Thank you @Lance Li-MSFTLavalava

© 2022 - 2025 — McMap. All rights reserved.