Android C/C ++ Development.

Source: Internet
Author: User

Does Android support C/C ++ development? Programs above the virtual machine are developed in Java,

However, the underlying layer can use C/C ++ to run some background programs. soket communication is not a big deal.

 

We plan to store a set of busybox, Bash, and command in rootfs,

The command can be completely static, and the shell in Android is too difficult to use.

It is too troublesome to compile C programs using Android. mk. I plan to compile a set of other methods to compile C Programs.

Ndk? Ndk is just a perfect JNI, so and Java programs can be easily packaged into the APK file.

 

Many people say that C Programs can only be compiled statically. That's a nonsense. If you use the cross-compilation tool in Android,

Use its environment for compilation. Naturally, you do not need to use static files.

 

The following provides a method to compile C/C ++ programs conveniently in the android environment.

Later, I will try to Port various C libraries to Android and share some porting experiences and methods.

 

First, make a configuration file by yourself, mainly for Android compilation and link parameters:

 

Cat zconfig. mk

Ifdef anddroid
Abionic = $ (a) bionic/libc/
Aoutlib = $ (a) Out/target/product/generic/obj/lib/
Cflags + =-I $ (a) bionic/libc/arch-arm/include-I $ (a) bionic/libc/include-I $ () bionic/libc/kernel/common-I $ () bionic/libc/kernel/arch-arm-C-fno-tions-wno-multichar-March = armv5te-mtune = XScale-msoft-float-FPIC-mthumb-interwork-ffunction- sections-funwind-tables/
-Fstack-protector-dctor arm_arch_5 _-d1_arm_arch_5t _-d1_arm_arch_5e _-d1_arm_arch_5te __/
-Include $ (a) System/CORE/include/ARCH/Linux-arm/androidconfig. h-dandroid-fmessage-length = 0-w-wall-wno-unused-dsk_release-dndebug/
-O2-wstrict-aliasing = 2-Finline-functions-fno-inline-functions-called-once-fgcse-after-reload-frerun-CSE-after-loop/
-Frename-registers-dndebug-udebug-mthumb-OS-fomit-frame-pointer-fno-strict-aliasing-Finline-Limit = 64-MD

Libs + =-nostdlib-bdynamic-wl,-T, $ (a) build/CORE/armelf. x-wl,-dynamic-linker,/system/bin/linker-wl, -- GC-sections-wl,-Z, nocopyreloc

Libs + =-L $ (aoutlib)-wl,-rpath-link = $ (aoutlib)-LC-LM $ (aoutlib) crtbegin_dynamic.o-wl, -- no-undefined $ (a)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin /.. /lib/GCC/ARM-Eabi/4.2.1/interwork/libgcc. A $ (aoutlib) crtend_android.o
Endif

Next is a general makefile, which must contain zconfig. mk

Cat makefile

 

Ifdef target
Include zconfig. mk
Cross = $ (cross_compiler )-
Cc = $ (Cross) GCC
AR = $ (Cross) Ar
LD = $ (Cross) LD
Else
Cc = gcc
AR = ar
LD = LD
Strip = strip
Endif

Include + =-I ../-I ./

Obj_dir = OBJ/
Objects = $ (obj_dir) Hello. o
Target_output =./Hello

Cflags + =
ALL: $ (target_output) $ (test_output) $ (objects)

$ (Obj_dir) %. O: %. c
@-Mkdir-p $ (obj_dir)
$ (CC) $ (include) $ (cflags)-C $ <-o $ @

$ (Target_output): $ (objects)
$ (CC) $ (libs)-o $ (target_output) $ (objects) $ (ld_path) $ (libobjects)
@ Echo "make $ @ finished on 'date '"
Clean:
@ RM-F $ (target_output)
@ RM-RF OBJ

 

Then there is a hello. c

# Include <stdio. h>

Int main ()
{
Printf ("Hello World/N ");
Return 0;
}

 

 

Compile environment variables by yourself:

Cat setenv. Sh

 

Export Path = $ path:/Android/myandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin
Export cross_compiler = arm-Eabi
Export target = 1
Export anddroid = 1
Export A =/Android/mydroid/

 

 

Before compiling helloworld, make sure that you have compiled the android source code,

Check that the out directory contains the corresponding libraries and tools.

This mechanism is very flexible. You can easily compile the x86, ARM Linux, and Android versions.

 

Last executed

Make

 

 

After the compilation is successful, use ADB push to put the program into the virtual machine.

If you want to compile the dynamic library, there is a small difference. Next we will introduce it.

 

 

 

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.