Question Compiling with CSC

tmulherin

New member
Joined
Oct 13, 2021
Messages
2
Programming Experience
5-10
Hi. I've been searching and searching for the way to run csc in my src folder and have the output compiled in my bin folder. Is that possible?
 
Yes. Read the official documentation from MSDN

You can invoke the C# compiler by typing the name of its executable file (csc.exe) at a command prompt.

For .NET Framework projects, you can also run csc.exe from the command line. Every compiler option is available in two forms: -option and /option. In .NET Framework web projects, you specify options for compiling code-behind in the web.config file. For more information, see <compiler> Element.

If you use the Developer Command Prompt for Visual Studio window, all the necessary environment variables are set for you. For information on how to access this tool, see Developer Command Prompt for Visual Studio.

The csc.exe executable file is usually located in the Microsoft.NET\Framework\<Version> folder under the Windows directory. Its location might vary depending on the exact configuration of a particular computer. If more than one version of .NET Framework is installed on your computer, you'll find multiple versions of this file.




 
Yes. Read the official documentation from MSDN






Thank you so much for your fast reply. Unfortunately, the StackExchange solutions did not address what I want to do. The Microsoft Docs Output Compiler Options indicates that I should use -out without giving a clue as to how to use it. I tried this: csc -out ..\Bin\DefConsDemo.exe DefConsDemo.cs and I got "error CS2005: Missing file specification for '-out' option" I intend to set up a src and bin directory for each chapter of the text I am following then in a console window navigate to the src directory and then compile to the bin directory. I don't want VS to assume it is all one executable that needs a specific entry point. It is so easy to do with javac. How do I do it with csc?
 
It's supposed to be -out:..\Bin\DefConsDemo.exe . I see the typos in the documentation page where they failed to include the colons as part of the option. I'll file a bug.

Also, the first link about has this noted:
For csc.exe, any arguments are listed following the option and a colon. For example, the -doc option would be:
Console:
-doc:DocFile.xml

You can invoke the C# compiler by typing the name of its executable file (csc.exe) at a command prompt.
 
Last edited:
I don't want VS to assume it is all one executable that needs a specific entry point. It is so easy to do with javac. How do I do it with csc?
First misconception to fix is that it is VS doing the compilation. CSC.exe and Visual Studio are different entities. You can install just the .NET Framework SDK and get csc.exe but not have Visual Studio, and still be able to compile assemblies into executables, libraries, and modules. (Visual Studio can only compile assemblies into executables and libraries.)

Next, MSBuild.exe is essentially Microsoft's analog to Ant. (Inside Microsoft there was a short war between internal groups using NAnt -- an Ant like build system for .NET Framework vs. MSBuild.exe. You can see the results of who one that war. :) ) As your projects get more complex, putting in the build information into a .CSPROJ file which MSBuild can use is going to be the best way to contain that complexity. (Part of the reason MSBuild won because it supported doing other builds other than just C# projects, and so you get the various .*PROJ file extensions.) When you use the VS Solution Explorer, it goes and edits the .*PROJ files under the covers. The sad part though is that that we you build in Visual Studio, VS actually has it's own internal port version of the MSBuild engine that sometimes acts a little bit different from how the command line MSBuild.exe works. Over the past years, the delta between the two behaviors has become smaller and smaller due to the increased use of CI/DC systems where the build machine is using MSBuild.exe while developers are using VS on their desktops.

Anyway, I went into that diversion about MSBuild because of the documentation links I provided above tended to look more at MSBuild directives because as I previously said, more likely than not, you'll end up doing builds using a .*PROJ file to build your code instead of building at the command-line invoking csc.exe directly, or putting that command-line into a batch script or makefile.

Next, javac by default compiles each .java file into a .class file. Later you can group these .class files into modules or packages. This much like the way most professional C/C++ programmers will tell their compiler to build .c/.cpp files into .obj files which will later be linked together. They then later link the .obj files into a single executable or library. Csc.exe on the other hand compiles the .cs files into an assembly that might be an executable, library, or module. Executables need a single entry point because you can run these directly (as long as the .NET Framework is installed on the machine) by running typing in filename.exe, or by double clicking in the File Explorer without a requirement of a file association to be configured. The java .class files cannot be run directly just by typing filename.class, you'll need to type in java filename.class, or ensure that .class files associated with Java before being able to double click on them in the File Explorer.

If you want to do a build where each .cs file produces it's own executable, you'll need to script it using CMD batch script, PowerShell, makefiles. Alternatively you can also use MSBuild by using transforms.
 
Last edited:
Back
Top Bottom