Thoughts on C language portability

Source: Internet
Author: User

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

Related Article

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.