Codelog

foreach(Snippet aSnippet in CodeLog){ aSnippet.GetSolution(); }

Building C# applications from command prompt

with 2 comments

Ever wondered how things will be if there is no Visual Studio ?? Even the compilation of a C# application (especially windows based application) is kinda difficult. Dare to dream soo ? Lets try to build a window based application without visual studio’s source builder.

  • Open Visual Studio and create a new Windows Application, say Foo. Its target will be an executable (exe)
  • Grab some buttons in to the form (simplly. No specific use)
  • Now open your visual studio command prompt. This is not something really Alien. This is the ordinary command prompt with some pre applied commands, rather some paths and variables set
  • Go to the folder where you have created your project. Consider that I have Foo under my E:\tries. then, navigate to cd E:\tries\Foo\Foo always prefer creating solution in a seperate directory. Thats why Foo/Foo
  • Now try typing csc Program.cs and check what happenscsc Program.cs
  • You will get some error stating that it cannot find Form1. Allrite. Lets solve it. This occurs because you are trying to build Program.cs alone without Form1 (Open and check Program.cs file. You will see an object of Form1 inside the Main() method). Leave it.
  • Lets try building Form1.cs

    csc Form1.cs     

  • Again a build error pops up. This is due to the partail classes. Gosh. You can solve all these error by using the recursive build. i.e. Progam.cs actually needs Form1 which in turn needs something else. Try typing

    csc /recurse:*.cs

  • The above command will build all the .cs files in the way it needs. Now relax.
  • Sometimes you may get other errors, like assembly not referenced. How to resolve those ?? How to add assemblies in command prompt ??? There are loads of options for the csc. I have discussed some of them below

Recursively build files

To automaically build files in a specific order and to do it recursively, we can use

csc /recurse:*.cs

Attaching references

To attach a reference we can use the /reference: option (/r:) in short

csc /r: System.Windows.Forms.dll /recurse:*.cs

Specifying the output (target) for a project

We can even specify the type of a target and the name if it using the /out option

csc /r: System.Windows.Forms.dll /recurse: *.cs /out:Foo.exe

Specifying debug and release builds

If you wanna run a debug build, use the /define option

csc /r: System.Windows.Forms.dll /recurse:*.cs /out:Foo.exe /define:DEBUG

Change it to whatever your conditional parameter is. Running the above will enable all the traces and will be seen in the console itself.

There are loads more in the MSDN library. Check the compiler options for more.

Written by sudarsanyes

November 3rd, 2008 at 2:39 pm

2 Responses to 'Building C# applications from command prompt'

Subscribe to comments with RSS or TrackBack to 'Building C# applications from command prompt'.

  1. I remember this. We did C# programs back in college using csc.
    I think for Mono the command would be mcs.

    cnu

    4 Nov 08 at 12:12 am

  2. [...] won’t be any errors. On second thought, you can refer building a C# application to [...]

Leave a Reply