Through the first simple C ++/CLI instance, we have some knowledge about the basic syntax of C ++/CLI, some new keywords are also quite familiar. In fact, if you have a background in ISOC ++, learn about some. Net related concepts, and use vs2008 IDE for development, I believe you can now use C ++/CLI to write something simple and small.Program. Continue with the previous sample to learn more about its compilation and decompilation.
1) Compile
After vs2008or winsdkis installed, we have many tools, in which cl.exe is used to compile C ++CodeFor many cl.exe options, you can view msdn. Here we use cl.exe/CLR main. CPP is used to compile the previous instance. The/CLR option indicates the Il intermediate code to be compiled as managed, because we know that ISOC ++ can be directly compiled into local code, Main. CPP is the name of the C ++ file to be compiled. Other options are not explicitly specified. The default value is used. After the sub-command is executed, main.objand main.exeare stored in the same directory as main.cpp, and the corresponding main.exe is the Il intermediate code. The compilation process is as follows:
2) JIT compilation
When the language is Il intermediate language (we can consider it as a high-level assembler language first), there is still a distance from the real language, so why can the exe of the il intermediate language be executed? This is due. net Virtual Machine mechanism, when the Il intermediate language EXE runs, it is actually dependent on CLR first compiled into the real assembly code, and then run.
After the exe of the il intermediate language is directly released, the first execution of the user will compile the Il intermediate language into a real Assembly. We call this process JIT (Just In Time) compilation, JIT compilation considers the fact that some code may never be called during execution. It is not time-consuming and memory-consuming to convert all the msil in the portable executable (PE) file into local code, instead, the msil is converted as needed during execution and the generated local code is stored in the memory for subsequent calls in the process context to access. When a type is loaded and initialized, the loader creates a stub and attaches it to each method of the type. When a method is called for the first time, Stub gives control to the JIT compiler, which converts the msil of the method to the local code and modifies the stub) to direct it to the generated local code. In this way, subsequent calls to the JIT compiled method will be directly transferred to the local code.
Of course, the ngen.exe tool is also available. You can use this tool during installation to convert the EXE in the Il language into the real assembly code first, so that the EXE will be the same as our traditional exe, there is no first compilation at runtime. After ngen.exe is compiled, a local image (a file containing compiled processor-specific machine code) is generated and installed to the local image cache on the local computer. The Runtime Library CLR uses a local image from the cache, instead of using a real-time (JIT) compiler to compile the original assembly. It is best to set. dll ngender, but not exe, because the execution of .exe is started by the operating system, the operating system first starts CLR through a fixed function in EXE, and then gives control to CLR. The following is an instance that uses ngen.exeto upload the local image of main.exe:
3) Comparison between jitand ngen.exe
The code generated by the JIT compiler is bound to the process of accessing the compiler. ngen.exe can share compiled code among multiple processes.
4) Anti-il intermediate language
C ++/CLI is based on CLI and uses Il as the intermediate abstraction layer. Our c ++/CLI code is compiled into the Il intermediate language, from the Il intermediate language to the underlying compilation CLR, it helps us to improve our developers to the Il intermediate language layer, just like the underlying system, therefore, we only need to care about the Il intermediate language. The IL intermediate language is very similar to our advanced language, which greatly reduces the difficulty of understanding the underlying language. Use the command ildasm main.exe to counter the Il intermediate language, for example:
After running the command, open the ildasm window as follows:
In the ildasm window, we can see the definitions of nativeclass, valuestruct, and refclass, which are the Il intermediate language of CLI. Next, go deep into the compiled il intermediate language!
Complete!