Android uses C code to achieve dynamic and static links

Source: Internet
Author: User

Static link to the C file in ANDROID:

0. First, install the gcc-related compiler, which is not detailed in detail. The author uses mingw, or other compilers. Many installation instructions on the network are not described in this article.

1. Create a C code named "hello" and write the following content:
# Include <stdio. h>
Int main (){
Printf ("hello c !!! ");
Return 0;
}

2. Go to the directory specified in the c file, for example, d: \ test. Run the gcc hello. c or gcc hello. c-static-o hellostatic command to generate an executable exe file.

NOTE: If there is no new line end the file warning, you can press enter at the end of your programming.

3. Start the simulator and run the adb command (the adb command must be configured first, otherwise it may not support command errors). Create a c folder in the adb shell --> cd data --> mkdir c

4. Go to the directory specified in the c file, for example, d: \ test, Run D: \ test> adb push hellostatic.exe/data/c, and load the file into the c file,

5. Run the adb shell command to access the c file, for example: adb shell --> cd data --> cd c --> ls: Execute chmod 777 hellostatic.exe, for example, # chmod 777 hellostatic.exe (Set permissions)

6. continue to run the program through the./hellostatic.exe command. It will appear; 'Hello c !!! '

Summary: The executable files generated by static links are large and do not need to depend on any system library. Everything is self-contained.

Dynamic Link:
1. Run the command D: \ test> gcc-c Hello. c-o hello. o.
Because dynamic links are used, libraries of Linux in ANDROID must be used for links. These libraries are located in the/system/lib directory,

2. Go to the system folder and execute ls to list the files. One of them is the lib directory. Go to and list the files. There are also folders such as hw for various so files. To properly link the program, we copy all the library files in this directory (system/lib) to the local folder, run the adb pull command, such as C: \ Users \ Administrator> adb pull/system/lib d: \ LIB \ android_lib.

3

4. After the above image is executed, the following steps are similar to static links. Execute the command (install) adb push-> (control permission) adb shell chmod 777-> (run) adb shell/data/c/hello.exe

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.