Compiling and Linking the Hello World Application

Compiling the "hello world" application is simple, and you do not need Visual Studio .NET to do it. If you are a glutton for punishment and don't mind using a text editor and a debugger that is not integrated with your editor, the Microsoft.NET Framework SDK is all you need to create a simple application with .NET.

The following compilation instructions will work if you have Visual Studio .NET or just the .NET Framework SDK installed:

csc out:/Hello.exe /t:exe Hello.cs

Tip

If you get an error that says "csc is not recognized as an internal or external command, operable program or batch file", make sure that you have your environment configured correctly. There are a few environment variables that you need to set, and your path should include the bin directory for the .NET Framework SDK. See the .NET Framework SDK for more detailed information. If you have Visual Studio .NET, you can select Programs | Microsoft Visual Studio .NET | Visual Studio .NET Tools | Visual Studio .NET Command Prompt from the Start menu, and this will create a command prompt that has a fully configured environment.


The csc command invokes the C# compiler. The /out parameter specifies the name that you want to give the assembly that the compiler generates. Remember that the output of this compilation is not a traditional x86 executable. The output of any compilation with the C# compiler will either be a complete assembly, which includes metadata, MSIL code, and a manifest or it will be a module, which contains metadata and MSIL code that you can subsequently link using the csc command or the assembly linker (al) to create an assembly. The “/t” parameter, which is a short form for “/target”, specifies the type of binary file that the compiler will generate. Using /t:exe, as specified here will generate a console executable assembly. Using /t:winexe will generate an executable assembly with a GUI. Using /t:library will create an assembly that is a DLL, and using /t:module will create a module that must be linked into a multifile assembly before it will be usable.

Like most programming languages, if you use types from libraries, even the standard ones, you will need to specify which libraries those types came from. With languages like C and C++, you had the choice between static and dynamic linking. If you used static linking, the code that you referenced from the external library would be embedded into your generated executable so that the resulting application was not dependent on the library file at runtime. In other words, you did not have to redistribute a DLL with your executable. With dynamic linking, your linker simply inserted a reference to the DLL. Your executable was smaller because it did not include the code that you used from the DLL, but now you have to redistribute the library with your application. The linking process with .NET is similar to dynamic linking. In .NET, linking to an assembly is called referencing the assembly. If you are using a multifile assembly, you will only reference the file that contains the manifest for the assembly. With the C# compiler, you reference an assembly using the “/reference” command-line option, which can be abbreviated to “/r”. For instance, the hello world example that I did before used the Console class, which resides in the core, system assembly (mscorlib.dll). You do not have to reference this assembly explicitly because the C# compiler always implicitly references it. However, if you did want to explicitly reference the mscorlib assembly, you would do so as follows.

csc /out:Hello.exe /t:exe /r:mscorlib.dll Hello.cs

Note

You can prevent the compiler from automatically referencing this assembly using the nostdlib parameter as shown here: csc out:/Hello.exe /t:exe /nostdlib /r:mscorlib.dll Hello.cs. If you do use the nostdlib parameter, you must explicitly reference mscorlib.dll.


Of course, in most cases, you will not use the command-line tools in the .NET Framework SDK to compile your applications. You will compile your applications using Visual Studio .NET, which is much simpler. Because you will be using Visual Studio extensively starting in the next chapter, I will not cover it here.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.161.222