Compiling C# code from the command line
It”s well known that the C# compiler is the csc.exe and that you can find it on the installation folder of the .NET platform. What i”ve seen in these last days (specially since i”ve started my in-depth study of the C# language and have started discussing it with some collegues and friends) is that many people use the C# compiler and really don”t know what it does or what are the available options you can use when you compile a C# app.
For instance, i was astonished with the number of people who don”t know what the following line does:
csc /target:exe /reference:mscorlib.dll /out:HelloWorld.exe HelloWorld.cs
Maybe this is a classical example of why VS rottens the mind (and no, i won”t stop using VS – specially now, that I”m a proud owner of a resharper”s lic:),,)?
Even among those that know what it does (btw, it”s compiling the HelloWorld.cs file into an exe called HelloWorld.exe and saying that it needs the “all mighty” mscordlib.dll to do that), many don”t really understand why you can reduce it simply to this (besides saying that it”s magical):
csc HelloWorld.cs
So i thought I should write something about this (even though that i”m positive that there”s already lots and lots of info out there). Ok, so why do the previous 2 instructions produce similar results? Easy:
- by default, the csc.exe will always generate an executable. since i was trying to built an exe, then i can just remove the /target:exe from the command;
- mscorlib.dll is a special assembly since it contains several of the basic (or primitive) types that you use when you build a C# program. The C# compiler knows this and will always add a reference to this assembly. Notice that this might not be what you want (for instance, when you”re building a silverlight application, you need to compile your cs files agains Silverlight”s mscorlib.dll and not against the one that is installed with the full .NET version). In these scenarios, you can cancel the default behavior of the compiler by passing the /nostdlib parameter;
- since i wanted the assembly to have the same name as the file that i was compiling, then it”s safe to discard the /out parameter. When you do that (ie, when there”s no /out parameter), the compiler will name the final assembly with the name of the file that has the Main method (exe) or the name of the first cs file you”re compiling (dll).
Not too complicated, right? More dificult is understanding why you can compile an app that uses a type defined inside the System.Data.dll assembly without passing a reference to that assembly during compilation. well, it happens that the csc.exe can also receive a response file. A response file is simply a text file where you define a switch per line. For instance, if i wanted, i could transform my first example in a helloworld.rsp (notice the extension) file that would look like this:
/target:exe
/referente:mscorlib.dll
/out:HelloWorld.exe
You can make the compiler to use these settings by using the /@ switch:
csc @helloworld.rsp helloworld.cs
If you want, you can have several response files. One important thing: the C# compiler will always load any csc.rsp response files it finds. This means that you can simply rename the previous file to csc.rsp and the compiler will pick it up automatically, making the @ switch disposable(do keep in mind that in my example you really don”t need a response file since the parameters i”m passing are also automatically used by the compiler, as explained in the previous paragraphs).
Ok, interesting right? but still doesn”t answer my last question. It happens that the compiler currently searches for rsp files in two places: current working folder and the installation folder of the compiler. If you open that folder, then you”ll see that it has a csc.rsp file with references to several of the assemblies installed by the .NET (including the assembly i mentioned in the previous example). This file is special and is always picked up by the compiler. That”s why you can do what i was saying 2 or 3 paragraphs above…
One important gotcha: the csc.rsp file will always be loaded by the compiler. If you don”t want that, then pass the /noconfig switch to the C# compiler.
btw, do notice that you can add or remove assemblies, though i see no reason for removing any of the list. Some people think that by removing assemblies they”re improving the performance of the new compiled assembly. This is not true because the compiler is smart enough to only add references to types that are used in your C# code files.
Finally, another question that pops up quite frequently, is where do those assemblies come from? for instance, what happens when you have several versions of the .NET platform installed. To answer this, we must know the places where the C# compiler searches for the dll:
- working directory
- directory that contains the csc file (notice that inside this directory you”ll find copies of the dlls that exist on the GAC.)
- folders passed through the /lib switch
- folders defined on the LIB ambient variable
As always (or should i say, as amost always?) msdn has a good reference on the parameters you can pass to the compiler. Reading them garantees that you”re aware of all the options you can use when you compile your program. Don”t stop using VS (or another cool editor). However, do know what you”re doing!
iPhone Unlocked
I see that PureMobile.com have posted a video of an unlocked iPhone.
I wonder how long it will be now before we see these things here in Ireland and the UK. For now, I have to make do with my HTC Touch.
[YouTube:HgMUI85U3BM]
LINQPad: cool!
I was really looking for this sort of tool and I”ve found it via Scott Gu”s post. Awesome!
C#: where does the name come from?
This isn”t really a technical post, so you might want to skip it if you”re too busy :,,)
As I”ve said, i”ve been spending some time looking at C# and how it produces code that runs on the CLR. One of the things that has always puzzled me is why C# is called C#
Until now, I”ve heard one or two interesting theories. The first is old (at least, I recall having heard about it in the past): since C# is supposed to be a better C++, then some see the # symbol as being composed by four + symbols.
The second one says that # is really the number sign that is used for a musical sharp, implying that he current sound is half step above the other (which really means that C# would above the other languages).
Btw, I ”ve found both these theories presented on the C# professional book (chapter 1 is available online here).
And you? what do you think? have you heard any better explanation for its name?
GFI Webmonitor 4 released – not without problems
Recently GFI released GFI Webmonitor 4. We have upgraded a few sites without issue – but then we hit a problem.
On one particular site had installed GFI MailArchiver 4 as well as GFI Webmonitor 4. On this site, whilst the Webmonitor application appeared to function, the GUI administrator interface gave various errors relating to .NET not finding the application.
After a few days working with the team at GFI they reported the problem to be that the two applications shared the same IIS Port, 8017. How this got past testing amazes me, but there is a fix…. here it is:
- Stop the GFI MailArchiver Core Service
- Stop the GFI MailArchiver Core2 Service
- Stop the GFI WebMonitor Service
- Follow the procedure in this file.
- Start the GFI Webmonitor Service
- Start the GFI MailArchiver Core2 Service
- Start the GFI MailArchiver Core Service
TestMatrix Inline Test Running
TestMatrix has a great feature that allows a user to run a test while in the Visual Studio environment. By right-clicking in a file, on the file, a folder, or using the Test Explorer tool window, a developer can run a unit test and see the color-coded results in the test file (through a highlighting in the left sidebar) or in the test explorer.
What makes it nice is that a user can run a unit test and view the results directly in the open window. I like being able to execute tests at a folder level, because I am often working in a specific folder at one time, working on the code and the tests. Plus, the visualization makes it more clear, and it has an icon that appears in the side that shows the error.
Furthermore, code coverage icons state what code wasn’t hit, and what code took the longest to run.
ASP.NET AJAX: Alessandro''s book is out
Alessandro Gallo”s (aka Garbin) book ASP.NET AJAX in Action is out and you can order it from here. I”ve met Alessandro on MSN during the early days of ATLAS and we”ve had several cool discussions about the platform. I”m sure he has done a fine job with this book. So, if you”re looking for a good english book on AJAX, then go ahead and check this one here (btw, if you prefer to read in portuguese, then don”t forget that i”ve also writen a book for FCA – sorry for the shameless publicity, but i just couldn”t resist
,,).
How to check your IP, with a difference!
For ages I have used whatismyip.com and dnsstuff.com to check the IP I am presenting to the world.
I was getting bored of this, but now thanks to www.moanmyip.com I can hear the IP read out by a sexy girl.
Horrah!
Recent Comments