The use of the C # compiler CSC

Source: Internet
Author: User

Do not use the IDE, directly using the compiler to process the source code, and then generate the corresponding EXE file, click to execute. The following is the use of C # compiler CSC.

Configuration of the CSC compiler

Use Notepad to write a C # program, save the extension to CS and store it in the D drive. This file will be a sample in this example. The code is to write "I love u" to the command line.

Using System;namespace wrox{public class myfirstclass{static void Main () {Console.WriteLine ("I Love u!"); Console.readkey (); return;}}

Running the CSC compiler directly prompts you not to find the command, first to configure the environment variable:

On the installation path of the. NET Framework, you can see the folders that identify the versions of the. NET Framework that are already installed on this machine, and select the folder that is written with the latest version to enter. such as the. NET framework version in this machine is "v4.0.30319". The specific directory is:

C:\Windows\Microsoft.NET\Framework\v4.0.30319

Put this directory in the PATH environment variable of the system.

You can now compile the CS file by using the CSC command. After setting the environment variable, CMD needs to be restarted before the CSC command can be executed.

The cmd switch to the D disk, you can directly compile the Test.cs source file. Once the compilation is complete, a corresponding Test.exe executable program is available and the click is ready to run.

CSC's Compile command

1./refrence

This option enables the current compilation project to use the public type information in the specified file. This option is important for beginners. The shorthand for this option is/R. You must refer to all the files imported using the "using" keyword in your program code, and if you use a class library of your own in your program, you must also reference it at compile time.

Example: compiling a file and referencing the file used in the program

(Note: One of the MyExec.exe and MyLibrary.dll was created by himself)

2./target

This option is to tell the compiler what type of output file you want to get. Unless you use the/target:module option, the output files created by other options contain a compilation list. The Assembly list stores information about all the files in the compilation. In one command line, if multiple output files are generated, but only one assembly list is created and stored in the first output file.

Here are 4 ways to use/target:

/target:exe Creating an executable (EXE) console application

/target:library Creating a code base (DLL)

/target:winexe Creating a Windows program (EXE)

/target:module Creating a module (DLL)

Example:

Csc/target:exe MyProj.cs//Create an EXE file Csc/target:winexe myProject.cs file://Create a Windows program Csc/target:library MyProject.cs file://Create a code base csc/target:module MyProject.cs file://Create a module

3./debug

This option is used during debugging, and when the debugger enables this option to debug his or her own program, a. pdb file is created and various debugging information is written to this file. There are 2 options to specify the type of debugging:

/debug [+/-]: Elected with/debug + will create a. pdb file and store debug information inside;/debug-is a default setting that does not produce any debug information.

/debug:[full/pdbonly]: When using/debug:full is to create the default debugging information, a bit like the/debug+ option. The/debug:pdbonly option is to create a. pdb file, and you can only use source code debugging in the Debugging tool.

Example: Compiling Hello.cs and creating debug information for Hello.cs

  

4./define

This option defines a symbol in the program that uses the # define preprocessor in the source program to indicate that the function is the same and that the symbol remains defined until the #undef indicator in the source file deletes the definition or the compiler has reached the end of the file . You can use the/D shorthand instead.

Example: The following is the source program for My.cs

If you compile with csc/define:final My.cs, "final Build" will be displayed, and if there is no/define, the "Trial build" will be displayed after compiling.

  

The use of the C # compiler CSC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.