I believe that students who have learned C language will see the characteristics of C language in the book: portability. But what is portability? How can it be transplanted? How can c be transplanted? For beginners, it may be a mysterious word that is often encountered. I want to use this article to express my thoughts on portability.
First of all, we will lay the groundwork here. Those who have studied Win32 programming must have heard of APIApplication Program Interface ). Let me talk about the API first. For programmers, an API is an interface provided by the system. Any system call involved must be completed through an API. Different operating systems have different sets of APIS, that is, the interfaces called by different operating systems are completely different. Therefore, we cannot port data at the API layer.
Next I will talk about portability. As the name suggests, it can be transplanted from one platform to another. However, we must be clear that the transplantation is based on the operating system. However, we need to note that applications cannot be directly transplanted at the level of level 2 based on different operating system platforms. We can only think about portability at the code layer. At the API level, due to the naming rules and system calls of various operating systems, it is unlikely to achieve portability at the API level. How can we achieve portability? First, let's take a look at the code portability on mainstream Windows and Linux platforms. Is there any way to solve this problem? The answer is: Abstraction of an intermediate layer between platforms based on most requirements. In the middle layer, the middle layer shields the underlying details. In our programmer's view, the C language library is such an intermediate layer. In each platform, the functions in the C standard library are the same by default, which can basically be transplanted. However, for the C library itself, its internal implementation is completely different under various operating system platforms, that is to say, the C library encapsulates the implementation details of the operating system API within it. Therefore, C language provides code-level portability, that is, This portability is achieved through the middle layer of C language. Of course, we can see that the above portability is conditional, and the C language itself cannot be completely portable. Why? In our programs, we often call system APIs. Because these APIs are not encapsulated in C, we can only use their original APIs, if the original APIs have different names in different operating systems, they cannot be transplanted across platforms. Therefore, we still need some other means to write completely cross-platform programs. For example, work hard in our code. The following code can help us achieve portability between platforms: # ifndef _ WINDOWS _ CreateThread (); // create a thread in windows # else Pthread_create (); // create a thread in Linux # endif uses the same precompiled macro for header files. For example, # ifndef _ WINDOWS _ # include <windows. h> # else # include <thread. h> # endif can be transplanted. During compilation, you only need to use # define to compile the program on that platform. To sum up, we use various languages such as C, C ++ as the middle layer to achieve a certain degree of portability. Today, cross-platform programs of languages are implemented in this way. However, different platforms still need to be re-compiled.
This article is from the "HelloWorld" blog, please be sure to keep this source http://vanshell.blog.51cto.com/890307/417068