Android. mk File Analysis

Source: Internet
Author: User
An android. mk
File is used to describe your source code to the compilation system. Specifically: The file is GNU
A small part of makefile will be parsed once or multiple times by the compilation system. You can
File defines one or more modules. You can also use the same source code file in several modules. The compilation system handles many details for you. For example, you do not need
Lists header files and dependent files. The ndk compilation system automatically handles these problems for you. This also means that after you upgrade the ndk, you should get the new toolchain/platform support
And does not need to change your android. mk file.

 

Let's take a look at a simple example: A simple "Hello World", such as the following file: sources/helloworld. c sources/helloworld/android. MK corresponding android. the MK file will look like the following: ---------- cut here ---------------- local_path :=$ (call my-DIR) include $ (clear_vars) local_module: = helloworldlocal_src_files: = helloworld. cinclude $ (build_shared_library) ---------- cut here ------------------ Let's explain these lines of code: local_path :=$ (call my-DIR) an android. MK file must first define the Loc Al_path variable. It is used to search for source files in the Development tree. In this example, the macro function 'my-dir' is provided by the compilation system and used to return the current path (that is, the directory containing the Android. mk file ). Include $ (clear_vars) clear_vars is provided by the compilation system, specifying
Makefile clears many local_xxx variables for you (such as local_module, local_src_files,
Local_static_libraries, etc., except local_path. This is necessary because all the compilation control files are in the same GNU
In the make execution environment, all variables are global. Local_module: = The helloworldlocal_module variable must be defined to identify each module you describe in the android. mk file. The name must be unique and contain no spaces. Note that the compilation system automatically generates the appropriate prefix and suffix. In other words, a shared library module named 'foo' will generate the 'libfoo. so' file. Local_src_files: = helloworld. c local_src_files must contain the C or C ++ source code files to be compiled and packaged into the module. Note that you do not need to list header files and contained files here, because the compilation system will automatically find the dependent files for you; just list the source code files directly transmitted to the compiler.

 

Add local programs or libraries in Android. These programs and libraries have nothing to do with their paths, but only with their android. mk files. Android. mk is different from a general makefile. It has a unified Writing Method and mainly contains some common macros of the system.

 

Multiple executable programs, dynamic libraries, and static libraries can be generated in an android. mk file.

 

1. Compile the Application Template: # test exelocal_path :=$ (call my-DIR) # include $ (clear_vars) local_src_files: = Main. clocal_module: = test_exe # local_c_includes: = # local_static_libraries: = # local_shared_libraries: = include $ (build_executable) (cainiao-level solution
Note: = refers to the value assignment, and $ refers to the value of a variable.) Add the source file path to local_src_files, local_c_includes
The path of the header file to be included in the file. local_static_libraries is added to the name of the static library (*. A) to be linked.
Name: name of the dynamic library (*. So) to be linked in local_shared_libraries. local_module indicates the final name of the module.
Build_executable indicates compiling in an executable program.

 

2. Compile the static library template: # test static liblocal_path :=$ (call my-DIR) include $ (clear_vars) local_src_files: =/helloworld. clocal_module: = libtest_static # local_c_includes: = # local_static_libraries: = # local_shared_libraries: = include $ (build_static_library)

In general, it is similar to the above. build_static_library indicates compiling a static library.

 

3. Compile the dynamic library template: # test shared liblocal_path :=$ (call my-DIR) include $ (clear_vars) local_src_files: =/helloworld. clocal_module: = libraries: = false # local_c_includes: = # local_static_libraries: = # local_shared_libraries: = include $ (build_shared_library) is similar to the above. build_shared_library indicates compiling a static library.

 

The above three generation results are as follows, and the generic changes according to the specific target: out/target/product/generic/obj/executableout/target/product/generic/obj/static_libraryout/target/product/generic/obj/shared_library the target folders of each module are: executable program: xxx_intermediates static library: xxx_static_intermediates dynamic library: xxx_shared_intermediates

 

In addition, in the Android. mk file, you can specify the final target installation path, which is specified using local_module_path and local_unstripped_path. Use the following macro to select different file system paths: target_root_out: indicates the root file system. Target_out: indicates the system file system. Target_out_data: indicates the data file system. Usage example: cal_module_path: = $ (target_root_out)
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.