The process of compiling .cs files into .dll using the csc command
Many times, we need to compile the .cs file into .dll file separately, and the operation is as follows:
Open the command window -> Enter cmd to console ->cd C:\WINDOWS\\Framework\v1.1.4322
Go to the directory where you installed ->Execute the csc command csc /target:library ->Create a .dll file with the corresponding name in this directory (prerequisite: put the .cs file in the directory C:\WINDOWS\\Framework\v1.1.4322)
There are many ways to csc commands, please refer to the following
Translated to generate
csc compile to produce
csc /target:library compile and create
csc /out: Compile all C# files in the current directory by using optimization and definition of DEBUG symbols. The output is
csc /define:DEBUG /optimize /out: *.cs Compile all C# files in the current directory to produce a debug version. No logos or warnings are displayed
csc /target:library /out: /warn:0 /nologo /debug *.cs Compile all C# files in the current directory into (one DLL)
csc /target:library /out: *.cs compile to produce
csc /target:library This is the command we use the most. In fact, it can be simply written as csc /t:library . Another way of writing is csc /out: /t:library , which can specify the output file name by itself.
csc /out: /t:library , the function of this is to install two cs files into a .dll file
csc is not an internal or external command, nor is it a runnable program solution
For VisualStudio2005
1: Right-click "My Computer"--"Properties"--"Advanced"--"Environment Variable"--"System Variable"
Add path to PATH: C:\WINDOWS\\Framework\v2.0.50727\
2: Execute directly in the corresponding folder directory of the cs in the dos environment
Path=C:\WINDOWS\\Framework\v2.0.50727\
3: VisualStudio2005 command prompt
Start--》Program---》Microsoft Visual Studio2005---->Visual Studio Tools--->VisualStudio2005 Command Prompts
Copy the cs file to C:\Program Files\Microsoft Visual Studio 8\VC\
4:C:\
Join: C:\WINDOWS\\Framework\v2.0.50727\
vs2008
C:\WINDOWS\\Framework\v2.0.50727\ is version 2.0
If the compiled .cs file is using; if the compiled cannot be completed, if you do not use the LINQ syntax to delete using; otherwise, call C:\WINDOWS\\Framework\v3.5
Under the same SLN, class directly accesses the class library.
Encapsulate to DLL. Import DLL Add using namespace to access.
1. Dynamic link library
What is a dynamic link library? The three letters of DLL must be very familiar to you. It is the abbreviation of Dynamic Link Library. Dynamic Link Library (DLL) is an executable file as a shared function library. Dynamic linking provides a way to enable a process to call functions that do not belong to its executable code. The executable code of a function is located in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the processes using them. DLLs also help share data and resources. Multiple applications can access the contents of a single DLL copy in memory at the same time.
Like most programmers, you must have used DLLs very much. I have also felt the mistakes in programming and coding. Today I want to discuss with you a topic: how to create and call DLL (dynamic link library) in C#. In fact, in a large sense, DLL allows me to organize and write our applications more flexibly. As a software designer, I can achieve a high code reuse effect based on it. Next, I will introduce how to create and call DLLs in C#.
2. Preparation
We need to give a brief introduction to what we want to do next. In this article, we will use C# language to create a dynamic link library called . In this dynamic link library file, we will provide two functions: one is to exchange their values for two parameters, and the other is to find the greatest common divisor of the two parameters. Then create an application using this DLL. Run and output the result.
3. Create a DLL
Let's create the following three C# code files:
1、
using System; namespace MyMethods { public class SwapClass { public static bool Swap(ref long i,ref long j) { i = i+j; j = i-j; i = i-j; return true; } } }
2、
using System; namespace MyMethods { public class MaxCDClass { public static long MaxCD(long i, long j) { long a,b,temp; if(i>j) { a = i; b = j; } else { b = i; a = j; } temp = a % b; while(temp!=0) { a = b; b = temp; temp = a % b; } return b; } } }
It should be noted that when we make these two files, we can use Visual or other text editors, even Notepad. Although these two files are not in the same file, they belong to the same namespace (namespace), which provides convenience for us to use these two methods in the future. Of course, they can also belong to different namespaces, which is completely OK, but we only need to reference two different namespaces when we apply them, so the author suggests that it is better to write it under one namespace.
The next task is to turn these two cs files into the DLL files we need. The method is as follows: On the operating system with Framework installed, we can find the directory in the directory where Windows is located. The C# compiler is provided under this directory, run: csc /target:library /out: . After completion, you can find the file we just generated in this directory. The /target:library compiler option notifies the compiler to output the DLL file instead of the EXE file. The /out compiler option followed by the file name is used to specify the DLL file name. If /out is not followed by the file name, the compiler uses the first file () as the DLL file name. The generated file is a file.
OK! The task of creating a dynamic link library file has been completed. Now is the time for us to enjoy the fruits of our labor. Below I will introduce how to use the dynamic link library file we have created. 4. Using DLL, let’s simply write a small program to test whether the two methods we just wrote are correct. OK, let’s follow me:
using System; using MyMethods; // Here we refer to the namespace we just defined. If the two files we just wrote in two different namespacesclass MyClient { public static void Main(string[] args) { if ( != 2) { ("Usage: MyClient <num1> <num2>"); return; } long num1 = (args[0]); long num2 = (args[1]); (ref num1,ref num2); // Note that the using directive at the beginning of the file allows you to reference the DLL method using an unqualified class name at compile time ("The result of swap is num1 = {0} and num2 ={1}",num1, num2); long maxcd = (num1,num2); ("The MaxCD of {0} and {1} is {2}",num1, num2, maxcd); } }
To generate an executable file, use the following command line:
csc /out: /reference:
The /out compiler option notifies the compiler to output the EXE file and specify the output file name (). The /reference compiler option specifies the DLL file referenced by the program.
V. Execution
To run the program, enter the name of the EXE file, followed by two numbers, for example: MyClient 123 456
6. Output
The result of swap is num1 = 456 and num2 = 123
The MaxCD of 456 and 123 is 3
7. Summary
Dynamic links have the following advantages:
1. Save memory and reduce swap operations. Many processes can use a DLL at the same time and share a copy of the DLL in memory. Instead, for each application generated with a static link library, Windows must load a copy of the library code in memory.
2. Save disk space. Many applications can share a copy of the DLL on disk. Instead, each application generated with a static link library has library code that is linked as a separate copy to its executable image. 3. It is easier to upgrade to DLL. When a function in a DLL changes, there is no need to recompile or relink the application using them as long as the parameters and return values of the functions have not changed. Instead, statically linked object code requires relinking the application when the function changes.
4. Provide after-sales support. For example, the display driver DLL can be modified to support monitors that were not available when the application was originally delivered.
5. Support multilingual programs. As long as the program follows the function's calling convention, programs written in different programming languages can call the same DLL function. The program must be compatible with DLL functions in the following ways: the order in which the function expects its parameters to be pushed onto the stack, whether the function or the application is responsible for cleaning the stack, and whether any parameters are passed in the register.
6. Provides a mechanism to extend the MFC library class. Classes can be derived from existing MFC classes and placed in the MFC extension DLL for use by MFC applications.
7. Make the creation of international versions easy to complete. Creating an international version of your application becomes much easier by putting resources into a DLL. You can place strings for each language version of the application into a separate DLL resource file and have different language versions load the appropriate resources.
One potential drawback of using DLL is that the application is not independent; it depends on the existence of a separate DLL module