test为可执行程序,libtest为库。在Android中编译并运行它们方法如下,首先要下载NDK。
参考了http://blog.csdn.net/L____J/archive/2010/08/04/5787759.aspx的方法,在此感谢。
1. 将库编译为动态库时:
test中仅有一个文件main.c
- #include <stdio.h>
- #include "libtest.h"
- static char s_string[] = "Study hard!";
- int main()
- {
- printf("%s/n%d/n", s_string, ret8());
- return 0;
- }
Makefile内容如下
- BASE_PATH = /home/apuser/Downloads/android-ndk/android-ndk-r4
- PATH_PREFIX = $(BASE_PATH)/build/platforms/android-8/arch-arm/usr/
- CC = $(BASE_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-eabi-gcc
- LD = $(BASE_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-eabi-ld
- INCLUDES= -I $(PATH_PREFIX)/include /
- -I/home/apuser/Downloads/android-ndk/android-ndk-r4/libtest/
- CFLAGS = -L $(PATH_PREFIX)/lib
- LDFLAGS += -nostdlib -lm -lstdc++ -lc -ldl -Bdynamic -Wl,-dynamic-linker,/system/bin/linker
- LDFLAGS += /home/apuser/Downloads/android-ndk/android-ndk-r4/libtest/libtest.so
- EXTRA_OBJS = $(PATH_PREFIX)/lib/crtbegin_dynamic.o $(PATH_PREFIX)/lib/crtend_android.o
- test: main.c
- $(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(EXTRA_OBJS) $(LDFLAGS)
- clean:
- rm -rf test *.o
libtest中有头文件libtest.h和libtest.c
- #ifndef _RET_8_H_
- #define _RET_8_H_
- int ret8();
- #endif
- #include "libtest.h"
- int ret8()
- {
- return 8;
- }
Makefile内容如下
- ASE_PATH = /home/apuser/Downloads/android-ndk/android-ndk-r4
- PATH_PREFIX = $(BASE_PATH)/build/platforms/android-3/arch-arm/usr/
- CC = $(BASE_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-eabi-gcc
- CCFLAGS = -I$(PATH_PREFIX)/include /
- -fpic /
- -mthumb-interwork /
- -ffunction-sections /
- -funwind-tables /
- -fstack-protector /
- -fno-short-enums /
- -D__ARM_ARCH_5__ /
- -D__ARM_ARCH_5T__ /
- -D__ARM_ARCH_5E__ /
- -D__ARM_ARCH_5TE__ /
- -Wno-psabi /
- -march=armv5te /
- -mtune=xscale /
- -msoft-float /
- -mthumb /
- -Os /
- -fomit-frame-pointer /
- -fno-strict-aliasing /
- -finline-limit=64 /
- -DANDROID /
- -Wa,--noexecstack /
- -O2 /
- -DNDEBUG /
- -g /
- LDFLAGS = -nostdlib -Wl,-soname,libtest.so -Wl,-shared,-Bsymbolic -Wl,--whole-archive -Wl,--no-whole-archive -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-rpath-link=$(PATH_PREFIX)lib
- EXTRA_OBJS = $(BASE_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/libgcc.a $(PATH_PREFIX)/lib/libc.so $(PATH_PREFIX)/lib/libstdc++.so $(PATH_PREFIX)/lib/libm.so
- libtest.so: libtest.o
- $(CC) $(LDFLAGS) $^ $(EXTRA_OBJS) -o $@
- libtest.o: libtest.c
- $(CC) $(CCFLAGS) -c $^ -o $@
- clean:
- rm *.so *.o
启动模拟器后,在ubuntu的shell中输入
adb push test /data/
adb push libtest /data/
adb shell chmod 777 /data/test
在模拟器的shell中
cd /data/
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
./test
注意
EXTRA_OBJS = $(BASE_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/libgcc.a
libgcc.a需放在其它库的前面,否则程序在运行时会有无法链接的提示,但在链接时没有报错。
2. 将库编译为静态库时
test的Makefile此时为将
- LDFLAGS += /home/apuser/Downloads/android-ndk/android-ndk-r4/libtest/libtest.so
改为
- LDFLAGS += /home/apuser/Downloads/android-ndk/android-ndk-r4/libtest/libtest.a
即可
libtest的Makefile此时为
- BASE_PATH = /home/apuser/Downloads/android-ndk/android-ndk-r4
- PATH_PREFIX = $(BASE_PATH)/build/platforms/android-8/arch-arm/usr/
- CC = $(BASE_PATH)/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-eabi-gcc
- INCLUDES= -I $(PATH_PREFIX)/include
- CCFLAGS = -L $(PATH_PREFIX)/lib /
- -fPIC /
- -Wall
- libtest.a: libtest.o
- $(AR) -rcs $@ $^
- libtest.o : libtest.c
- clean:
- rm *.a
3. test的Makefile也可通过automake工具生成
首先执行autoscan
将生成的configure.scan重命名为configure.ac
configure.ac内容修改如下
- AC_PREREQ([2.65])
- AC_INIT([test], [1.0.0], [-])
- AM_INIT_AUTOMAKE
- AC_CONFIG_SRCDIR([test.c])
- AC_CONFIG_HEADERS([config.h])
- # Checks for programs.
- AC_PROG_CC
- # Checks for libraries.
- # Checks for header files.
- #AC_CHECK_HEADERS([string.h])
- # Checks for typedefs, structures, and compiler characteristics.
- # Checks for library functions.
- AC_OUTPUT([Makefile])
Makefile.am内容如下
- AUTOMAKE_OPTIONS = foreign
- bin_PROGRAMS = test
- test_SOURCES = test.c
依次执行
aclocal
autoheader
autoconf
automake --add-missing
编写脚本build.sh,内容如下
- CC=arm-eabi-gcc /
- LD=arm-eabi-gcc /
- CFLAGS="-I/home/apuser/Downloads/android-ndk/android-ndk-r4/build/platforms/android-8/arch-arm/usr/include" /
- LDFLAGS="-L/home/apuser/Downloads/android-ndk/android-ndk-r4/build/platforms/android-8/arch-arm/usr/lib -nostdlib -lm -lstdc++ -lc -ldl -Bdynamic -Wl,-dynamic-linker,/system/bin/linker /home/apuser/Downloads/android-ndk/android-ndk-r4/build/platforms/android-8/arch-arm/usr/lib/crtbegin_dynamic.o /home/apuser/Downloads/android-ndk/android-ndk-r4/build/platforms/android-8/arch-arm/usr/lib/crtend_android.o" /
- ./configure /
- --build=i386-linux /
- --host=arm-elf-linux /
执行脚本build.sh后编译即可。
另有Funambol的脚本,也可使用,内容如下
- command for C compiller:
- ./configure --host=arm-eabi CC=arm-eabi-gcc CPPFLAGS="-I$ANDROID_ROOT/build/platforms/android-5/arch-arm/usr/include/" CFLAGS="-nostdlib -fno-exceptions -fno-rtti -Wno-multichar" LDFLAGS="-Wl,-rpath-link=$ANDROID_ROOT/build/platforms/android-5/arch-arm/usr/lib/ -L$ANDROID_ROOT/build/platforms/android-5/arch-arm/usr/lib/" LIBS="-lc"
- command for C++ compiller:
- ./configure --host=arm-eabi CXX=arm-eabi-g++ CPPFLAGS="-I$ANDROID_ROOT/build/platforms/android-5/arch-arm/usr/include/ -nostdlib -fno-exceptions -fno-rtti -Wno-multichar" LDFLAGS="-Wl,-rpath-link=$ANDROID_ROOT/build/platforms/android-5/arch-arm/usr/lib/ -L$ANDROID_ROOT/build/platforms/android-5/arch-arm/usr/lib/" LIBS="-lc"