C compiling module in Android Project

Source: Internet
Author: User

 

The android compiling environment is complex, and unlike the normal compiling environment: Only makefile files are available in the top-level directory, while each other component uses the unified standard android. mk. android. the MK file itself is relatively simple, but it is not a familiar makefile, but a lot of processing through the android compilation system. Therefore, it is still complicated to understand the links, however, the advantage of this method is to write a new android. MK to add a new component for Android is relatively simple. Compiling Java programs can be completed directly using the eclipse integrated environment, which will not be repeated here. The following is a small example of how to describe C/C ++ in Android
Add the hello World:
1. Create the hello directory under the $ (your_android)/Development Directory. $ (your_android) indicates the directory where the android source code is located.
-# Mkdir $ (your_android)/development/Hello
2. Write the hello. c file in the $ (your_android)/external/Hello/directory. The content of Hello. C is of course the classic helloworld program:
# Include <stdio. h>

Int main (INT argc, char ** argv)
{
Printf ("Hello world! /N ");

Return 0;
}
 

 
3. Write the Android. mk file in the $ (your_android)/external/Hello/directory. This is the standard name of Android makefile. Do not change it. For the format and content of the android. mk file, refer to other existing Android. mk files. The content of the android. mk file for the helloworld program is as follows:
Local_path: = $ (call my-DIR)

Include $ (clear_vars)

Local_src_files: = Hello. c

Local_module_tags: = optional

Local_module: = helloworld

Include $ (build_executable)
 

Note that the above local_src_files are used to specify the source file. local_module specifies the name of the module to be compiled, which will be used in the next step. include $ (build_executable) indicates that the file will be compiled into an executable file, if you want to compile it into a dynamic library, you can use build_shared_library, which can be in $ (your_android)/build/CORE/config. mk.
4. Return to the top-level directory of the android source code for compilation:
# Cd $ (your_android) & make helloworld
 

Note that the target name helloworld in make helloworld is the module name specified by local_module In the android. mk File above. The compilation result is as follows:
Target thumb C: helloworld <= Development/Hello. c

Target executable: helloworld (Out/target/product/generic/obj/executables/helloworld_intermediates/linked/helloworld)

Target non-prelinked: helloworld (Out/target/product/generic/symbols/system/bin/helloworld)

Target Strip: helloworld (Out/target/product/generic/obj/executables/helloworld_intermediates/helloworld)

Install: Out/target/product/generic/system/bin/helloworld
 

 
5. As shown in the preceding compilation results, the compiled executable files are stored in the out/target/product/generic/system/bin/helloworld directory.

 

Start the android simulator and use the following command to push the file to the android simulator:

Note: put the tools directory in the SDK directory into the path environment variable.

ADB shell mkdir/dev/sample
ADB push Hello/dev/sample/Hello

ADB shell chmod 777/dev/sample/Hello

Create the/dev/sample directory, upload the compiled hello, and change "hello" to executable.

Go to the command line mode and enter the android shell environment:

ADB Shell

# Cd/dev/sample

#./Hello

The android compiling environment is complex, and unlike the normal compiling environment: Only makefile files are available in the top-level directory, while each other component uses the unified standard android. mk. android. the MK file itself is relatively simple, but it is not a familiar makefile, but a lot of processing through the android compilation system. Therefore, it is still complicated to understand the links, however, the advantage of this method is to write a new android. MK to add a new component for Android is relatively simple. Compiling Java programs can be completed directly using the eclipse integrated environment, which will not be repeated here. The following is a small example of how to describe C/C ++ in Android
Add the hello World:
1. Create the hello directory under the $ (your_android)/Development Directory. $ (your_android) indicates the directory where the android source code is located.
-# Mkdir $ (your_android)/development/Hello
2. Write the hello. c file in the $ (your_android)/external/Hello/directory. The content of Hello. C is of course the classic helloworld program:
# Include <stdio. h>

Int main (INT argc, char ** argv)
{
Printf ("Hello world! /N ");

Return 0;
}
 

 
3. Write the Android. mk file in the $ (your_android)/external/Hello/directory. This is the standard name of Android makefile. Do not change it. For the format and content of the android. mk file, refer to other existing Android. mk files. The content of the android. mk file for the helloworld program is as follows:
Local_path: = $ (call my-DIR)

Include $ (clear_vars)

Local_src_files: = Hello. c

Local_module_tags: = optional

Local_module: = helloworld

Include $ (build_executable)
 

Note that the above local_src_files are used to specify the source file. local_module specifies the name of the module to be compiled, which will be used in the next step. include $ (build_executable) indicates that the file will be compiled into an executable file, if you want to compile it into a dynamic library, you can use build_shared_library, which can be in $ (your_android)/build/CORE/config. mk.
4. Return to the top-level directory of the android source code for compilation:
# Cd $ (your_android) & make helloworld
 

Note that the target name helloworld in make helloworld is the module name specified by local_module In the android. mk File above. The compilation result is as follows:
Target thumb C: helloworld <= Development/Hello. c

Target executable: helloworld (Out/target/product/generic/obj/executables/helloworld_intermediates/linked/helloworld)

Target non-prelinked: helloworld (Out/target/product/generic/symbols/system/bin/helloworld)

Target Strip: helloworld (Out/target/product/generic/obj/executables/helloworld_intermediates/helloworld)

Install: Out/target/product/generic/system/bin/helloworld
 

 
5. As shown in the preceding compilation results, the compiled executable files are stored in the out/target/product/generic/system/bin/helloworld directory.

 

Start the android simulator and use the following command to push the file to the android simulator:

Note: put the tools directory in the SDK directory into the path environment variable.

ADB shell mkdir/dev/sample
ADB push Hello/dev/sample/Hello

ADB shell chmod 777/dev/sample/Hello

Create the/dev/sample directory, upload the compiled hello, and change "hello" to executable.

Go to the command line mode and enter the android shell environment:

ADB Shell

# Cd/dev/sample

#./Hello

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.