Cross-platform outlines

Source: Internet
Author: User
Tags amd processors

Article purpose

For cross-platform compilation, there are a lot of tutorials and explanations on the web, but the total feeling is incomplete, so I want to focus on the arrangement. But cross-platform compilation is a very broad issue, if you want to be all clear, there will be a lot of problems involved, many times to verify the literature will slow down the progress, so decided to hide unnecessary technical details, from the "superficial understanding" angle, explain the cross-platform problems.

Expected hidden Parts

1. Give examples. As mentioned in the article, there is a (1101 0010) machine code instruction, but actually does not have this instruction, just to give an example convenient to construct such an instruction.

2. Simplify the structure. The article said "executable program = Operating system head + the machine code to complete the task", in fact, the executable program is certainly not so divided, nor so simple, but from the macro point of view, I think such division can reflect a certain problem, but also reasonable.

Reader Locator

Compile entry-level players across platforms.

You can use shell scripts skillfully.

Guiding ideology

Simplify the real-world computer model and describe the problems encountered from a different perspective.

Some concepts that are not the same as traditional
Program Source = C + + built-in Operations + various library calls
int Main (intcharConst *argv[]) {    int prices[] = {7  3845};    // built-in operations    for C + + Std::sort (Prices, prices+5);    // calling the STL library    return 0 ;}
executable = OS header + machine code to complete task

computer = Operating system + Processor
Why can't I do "write once, run everywhere"
Specific Description:

why. exe files that are compiled on Windows cannot be run directly on Linux.

Android is a Linux-based system, why the executable program compiled on Linux cannot be run directly on Android.

Answer:

To be executed successfully, the executable program needs to meet two basic requirements (operating system header + machine code to complete the task).

One is the operating system level, you must inform the operating system to say: This program is not a normal program, it can be executed! For Windows, the word "Windows is great" needs to be written in the [operating system header], and for Linux it is necessary to write the word "the Best of Linux is" and for Android to write "Android I love You "and so on. So, a program that is compiled with VS will only be considered executable by Windows, but for Linux and Android, "What is this?" I do not know. ”

Second, processor-level, mainly by the two major processor manufacturers (Intel and AMD) control. CXX source code compiled executable program, the internal storage is machine code, directly execute the machine code, is the processor. But different processors interpret the same machine code differently. For example (1101 0010) This machine code, in Intel's view, this is a mov ax,ex instruction, but in AMD's view, this is a pop instruction. As a result, programs compiled on Intel processors cannot run directly on AMD processors.

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

Cross-compiling platform compile

It says that the program that compiles on win only runs on the win machine, and the program compiled on Linux only runs on Linux, what do you want to do to compile a program that can run on the phone? Use the cross-platform compiler.

how the cross-platform compiler works

From the source code to the executable program, can be divided into 2 steps (the following statement differs from most 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) to the. obj file generated by your own code to generate the final executable file.

The code that you write yourself is cross-platform, this is the C + + Standard Committee to the various compilers to see, without your worry;

The library files you use, a lot of them are implemented by the Assembly, not cross-platform, so each platform has its own implementation, this also do not worry about you.

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

Where to find the library file

On windows, download VS, you just need to write your own code in VS and find the library file task vs will do it for you.

Linux, download the gcc/g++, you only need to write your own code, find the library file task gcc/g++ will do for you.

On Mac, download Xcode, you just have to write your own code and look for the library files task Xcode will do for you.

But since you can't run a vs/gcc/xcode on Android, you need to go to Google's official website to download an android-specific compiler, the NDK. Then you just have to write your own code and look for the library files task that the NDK will do for you.

3. Summary

The basic concepts are so much, here are some examples of cross-platform compilation. Before you continue, make sure the NDK development environment is properly configured on your computer.

Cross-platform outlines

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.