Building native code applications and libraries
STEP 1
Building an application.
--------
0) set the environment variable PREBUILT to point to the Android prebuilt directory
export PREBUILT=<path_to_android_src>/prebuilt/<platform>
where you type in the actual path to the android source in place of <path_to_android_src>
and the platform you are using instead of <platform>: either linux-x86 or darwin-x86
1) Test the pndk install by building the hello world sample application:
cd <your_pndk_base>/samples/sample
make clean
make
The sample application uses hello.c to construct the hello binary, which you
can load and run on the ARM device. To achieve proper runtime behavior, verify
that:
* crtbegin_dynamic.o is the first linked object file
* crtend_android.o is last linked object.
Both are set by the config.mk file in pndk/config.
2) Test that this works correctly by attaching your ARM-based device to the USB
port and installing the application (hello) you just made by (in the commands
below # is the ARM device's shell prompt):
NOTE: need a development build so remount opens system permissions
adb remount
adb push hello system/app
adb shell
# cd system/app
# ./hello
Hello from the NDK; no user libraries.
# exit
3) You may also build the c++ binary hello_cpp.cpp into an application:
make -f Makefile.hello_cpp clean
make -f Makefile.hello_cpp hello_cpp
This uses the hello_cpp.cpp and hello_cpp.h files to construct the hello_cpp
binary application, which you can load and run on the ARM device. Note that
we do not provide for C++ exceptions thus you must use the -fno-exceptions flag
when compiling.
adb push hello_cpp system/app
adb shell
# cd system/app
# ./hello_cpp
C++ example printing message: Hello world!
# exit
STEP 2
Building and using a library
-------
Makefile.lib in pndk/sample shows how to make either a shared library or a
static library from the hellolibrary.c source. The example makes the libraries
libhello-shared.so and libhello-static.a .
Makefile.uselib then shows how to make an application that links against either
a shared or a static library. They examples shows how to build the two
applications use_hellolibrary-so and use-hellolibrary-a from the source
use_hellolibrary.c.
1) To make a shared library and an application that uses it:
make -f Makefile.lib clean
make -f Makefile.lib sharedlib
make -f Makefile.uselib clean
make -f Makefile.uselib use_hellolibrary-so
2) Copy the shared library libhello-shared.so to /system/lib (or the location
in which shared libraries are found by the kernel on your ARM-based device.)
adb push libhello-shared.so system/lib
You would not typically use the -shared or -static extensions in the filename,
but the distinction is important in the case where a static and shared library
are made in the same directory. Giving the files different names allows you to
override the link defaults that default to a static library of the same name.
3) The application, use_hellolibrary-so, can now be tested by loading and
running on the ARM device.
adb push use_hellolibrary-so /system/app
adb shell
# cd system/app
# ./use_hellolibrary-so
Library printing message: Hello from the NDK.
# exit
4) To make a static library:
make -f Makefile.lib clean
make -f Makefile.lib staticlib
make -f Makefile.uselib clean
make -f Makefile.uselib use_hellolibrary-a
5) Test the application use_hellolibrary-a by loading and running it on the ARM
device.
adb push use_hellolibrary-a system/app
adb shell
# cd system/app
# ./use_hellolibrary-a
Library printing message: Hello from the NDK.
# exit
SUMMARY:
---------
To make everything execute the following:
make clean
make
make -f Makefile.lib clean
make -f Makefile.lib
make -f Makefile.uselib clean
make -f Makefile.uselib
make -f Makefile.hello_cpp clean
make -f Makefile.hello_cpp hello_cpp
You should have:
* The libraries libhello-static.a and libhello-shared.so built, the latter
ready for installation,
* The applications hello, use_hellolibrary-a, and use_hellolibrary-so
available for installation on the ARM device.
那個設定path, toolchain. base rul 的 config.mk:
# Assumes PREBUILT is defined to point to the correct flavor of the prebuilt
# directory in the Android source tree
CC := $(PREBUILT)/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc
AR := $(PREBUILT)/toolchain/arm-eabi-4.2.1/bin/arm-eabi-ar
INC := -I$(NDK_BASE)/include/bionic/libc/arch-arm/include \
-I$(NDK_BASE)/include/kernel/include \
-I$(NDK_BASE)/include/bionic/libm/include \
-I$(NDK_BASE)/include/bionic/libm/include/arm \
-I$(NDK_BASE)/include/bionic/libc/include \
-I$(NDK_BASE)/include/bionic/libstdc++/include
LINK := -nostdlib -Bdynamic \
-Wl,-T,$(NDK_BASE)/config/armelf.x \
-Wl,-dynamic-linker,/system/bin/linker \
-Wl,-z,nocopyreloc \
-L$(NDK_BASE)/lib \
-Wl,-rpath-link=$(NDK_BASE)/lib \
$(NDK_BASE)/lib/crtbegin_dynamic.o
POSTLINK := $(NDK_BASE)/lib/crtend_android.o
%.o: %.cpp
$(CC) $(CFLAGS) -fno-exceptions -fno-rtti $(INC) -o $@ -c $<
%.o: %.c
$(CC) $(CFLAGS) $(INC) -o $@ -c $<
NDK_BASE 在 sample/Makefile :
NDK_BASE := ../..
但是好像找不到正確的 NDK_BASE 位置..那幾個 bionic 開頭的跟其他的 include 位置都不match。
download 官方 NDK 下來也一樣,
重新 mapping 一下好了:
INC
include/bionic/libc/arch-arm/include : bionic/libc/arm-arm/include
include/kernel/include :
include/bionic/libm/include \
include/bionic/libm/include/arm \
include/bionic/libc/include \
include/bionic/libstdc++/include
ref 一下其他的:
target thumb C: test_pthread_mutex <= system/extras/tests/bionic/libc/common/test_pthread_mutex.c prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-gcc -I system/extras/tests/bionic/libc -I out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates -I system/core/include -I hardware/libhardware/include -I hardware/libhardware_legacy/include -I hardware/ril/include -I dalvik/libnativehelper/include -I frameworks/base/include -I frameworks/base/opengl/include -I external/skia/include -I out/target/product/imx51_bbg/obj/include -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm/include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -c -fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -march=armv7-a -mfloat-abi=softfp -mfpu=neon -include system/core/include/arch/linux-arm/AndroidConfig.h -I system/core/include/arch/linux-arm/ -mthumb-interwork -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -DNDEBUG -g -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 -D_XOPEN_SOURCE=600 -DHOST -MD -o out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/common/test_pthread_mutex.o system/extras/tests/bionic/libc/common/test_pthread_mutex.c target Executable: test_pthread_mutex (out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/LINKED/test_pthread_mutex) prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/LINKED/test_pthread_mutex -Lout/target/product/imx51_bbg/obj/lib -Wl,-rpath-link=out/target/product/imx51_bbg/obj/lib -lc -lstdc++ -lm out/target/product/imx51_bbg/obj/lib/crtbegin_dynamic.o out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/common/test_pthread_mutex.o -Wl,--no-undefined -Wl,--fix-cortex-a8 /home/charles-chang/myandroid91/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/interwork/libgcc.a out/target/product/imx51_bbg/obj/lib/crtend_android.o target Non-prelinked: test_pthread_mutex (out/target/product/imx51_bbg/symbols/system/bin/test_pthread_mutex) out/host/linux-x86/bin/acp -fpt out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/LINKED/test_pthread_mutex out/target/product/imx51_bbg/symbols/system/bin/test_pthread_mutex target Strip: test_pthread_mutex (out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/test_pthread_mutex) out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/imx51_bbg/symbols/system/bin/test_pthread_mutex --outfile out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/test_pthread_mutex Install: out/target/product/imx51_bbg/system/bin/test_pthread_mutex out/host/linux-x86/bin/acp -fpt out/target/product/imx51_bbg/obj/EXECUTABLES/test_pthread_mutex_intermediates/test_pthread_mutex out/target/product/imx51_bbg/system/bin/test_pthread_mutex
沒有留言:
張貼留言