"Go" module for compiling C in Android project

Source: Internet
Author: User

Original URL: http://blog.csdn.net/Harrison_zhu/article/details/4057738

The Android build environment itself is complex, and unlike a normal compilation environment: only the top-level directories have makefile files, while each of the other component uses a uniform standard of android.mk. ANDROID.MK file itself is relatively simple, but it is not familiar with the makefile, but after the android itself compiled system of many processing, so to really understand the connection is more complex, but the advantage of this approach is to write a new android.mk to Android It would be easier to add a new component. Compiling a Java program can be done directly using Eclipse's integrated environment, which is not duplicated here. We are mainly for the C + + to illustrate, below a small example to illustrate how to add a C program in Android Hello World:
1. Create a Hello directory under the $ (your_android)/development directory, where $ (your_android) refers to the directory where the ANDROID source code resides.
-# mkdir $ (your_android)/development/hello
2. In the $ (your_android)/external/hello/directory to write hello.c files, HELLO.C's content 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 for Android makefile, do not change. The format and content of the Android.mk file can be referenced in other existing android.mk files, and the contents of the Android.mk file for the HelloWorld program are 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 is used to specify the source file, Local_module specifies the name of the module to be compiled, and the next step is to compile it; include $ (build_executable) means to compile into an executable file, If you want to compile into a dynamic library, you can use Build_shared_library, which is found in the $ (your_android)/build/core/config.mk.
4. Go back to the top-level directory for Android source code to compile:
# 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 results are as follows:
Target Thumb C:helloworld <= development/hello/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 above compilation results, the compiled executable is stored in the Out/target/product/generic/system/bin/helloworld directory

Launch the Android emulator and push the file to the Android emulator using the following command:

Note: To place the tools directory in the SDK directory in the PATH environment variable.

ADB Shell Mkdir/dev/sample
ADB push Hello/dev/sample/hello

ADB shell chmod 777/dev/sample/hello

First create the/dev/sample directory, then upload the compiled Hello, and finally change the hello to executable.

Then enter the command line mode to enter the Android shell environment:

ADB shell

#cd/dev/sample

#./hello

"Go" module for compiling C in Android project

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.