Cross-platform outline

Source: Internet
Author: User

Cross-platform outline
Purpose

For cross-platform compilation, there are many tutorials and explanations on the Internet, but they are incomplete, so I want to concentrate on it. However, cross-platform compilation is a very broad issue. If you want to make it all clear, there will be many problems involved, and multiple verification documents will also slow down the process, so you decide to hide unnecessary technical details, from the perspective of "not understanding", I would like to explain various problems encountered across platforms.

Estimated hidden parts

1. For example. As mentioned in the article, there is a (1101 0010) machine code command, but in fact there is no such command, just to facilitate the example and construct such a command.

2. simplified structure. The article said, "executable program = operating system header + machine code for task completion", in fact, the executable program is certainly not so segmented, it is not so simple, but from a macro perspective, I think this division can reflect certain problems and is reasonable.

Reader Positioning

Cross-platform compilation for entry-level players.

Be familiar with shell scripts.

Guiding Ideology

Simplify the computer model of the real world and describe problems from different perspectives.

Concepts different from traditional ones
Program source code = C/C ++ built-in operations + various library calls
Int main (int argc, char const * argv []) {int prices [] = {7, 3, 8, 4, 5 }; // c/c ++ built-in operation std: sort (prices, prices + 5); // call stl library return 0 ;}
Executable program = operating system header + machine code for task completion

Computer = operating system + Processor
Why can't I "Write it once and run it everywhere" 
Description:

The. exe file compiled on Windows cannot be directly run on linux.

Android is a linux-based system. Why can't I directly run executable programs compiled on linux.

A:

To successfully execute an executable program, you must meet two basic requirements (the operating system header + the machine code used to complete the task ).

First, at the operating system level, you must notify the operating system that this program is not a general program and can be executed! For Windows, you must enter "windows is great" in the [operating system header]. For linux, you must enter "linux is the best". For android, write "android I love you" and so on. Therefore, a program compiled with vs can only be considered Executable by windows, but for linux and android, "What is this? I don't know ."

The second is the processor layer, which is mainly controlled by two major processor vendors (Intel and AMD. The executable program compiled by the CXX source code stores the machine code and directly executes the machine code, that is, the processor. However, different processors have different interpretations of the same machine code. For example, the machine code (1101 0010) is an mov ax and ex command from Intel's perspective, but it is a pop command from AMD's perspective. Therefore, the program compiled on the Intel processor cannot be directly run on the AMD processor.

Therefore, to correctly execute a program, you must ensure that the operating system header information is correct + the machine code of the corresponding processor is correct.

Cross-platform compile

As mentioned above, programs compiled on Windows can only run on Windows machines, while programs compiled on linux can only run on linux, what should I do if I want to compile a program that can run on a mobile phone? Use a cross-platform compiler.

How does a cross-platform compiler work?

From the source code to the executable program, there are two steps (the following statement is different from most of the tutorials ).

The first step is to compile your own code into a. obj file;

The second step is to link the library files you use (such as stl) with the. obj file generated by your own code to generate the final executable file.

Among them, the code you write is cross-platform, which is provided by the c ++ Standards Committee for various compilers, so you don't have to worry about it;

A considerable part of the library files you use are implemented in Assembly rather than cross-platform. Therefore, each platform has its own implementation, so you don't have to worry about this.

Your job is to write your own code, find the library files of the corresponding platform, and tell the compiler where they are located. The compiler will generate a cross-platform executable file for you.

Where to find the library file

On Windows, after downloading vs, you only need to write your own code in vs, and find the task vs of library files for you.

On Linux, gcc/g ++ is downloaded. You only need to write your own code and find the job gcc/g ++ for library files.

After downloading xcode on Mac, you only need to write your own code. xcode will complete the task of searching for library files for you.

However, since it is impossible for you to run vs/gcc/xcode on Android, You need to download an android-specific compiler, that is, ndk, from the google official website. Then, you only need to write your own code and find the library file task ndk will complete for you.

3. Summary

There are so many basic concepts. Below are some cross-platform compilation examples. Before proceeding, make sure that the ndk development environment is correctly configured on your computer.

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.