linux ndk静态编译,linux ndk静态编译

一.移植Curl工具到Android环境步骤1.修改cURL源码下的mk文件。源码下面的Android.mk文件最后生成的是静态库libcurl.a,做如下修改(编译成动态库)。LOCAL_PRELINK_MODULE := false

LOCAL_MODULE:= libcurl

LOCAL_MODULE_TAGS := optional

Copy the licence to a place where Android will find it.

Actually, this doesn't quite work because the build system searches

for NOTICE files before it gets to this point, so it will only be seen

on subsequent builds.

ALL_PREBUILT += $(LOCAL_PATH)/NOTICE

$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)

$(copy-file-to-target)

include $(BUILD_STATIC_LIBRARY)

include $(BUILD_SHARED_LIBRARY)

2.配置编译环境(cd 到Android.mk同一目录,直接在控制台输入下列代码或者把下面代码弄成sh脚本执行)红色部分根据自己源码情况ANDROID_HOME=/home/zhoulc/android/ && \

NDK_HOME=/home/zhoulc/android/ndk && \

PATH="$ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \

./configure --host=arm-linux CC=arm-eabi-gcc --with-random=/dev/urandom \

CPPFLAGS="-I$NDK_HOME/platforms/android-8/arch-arm/usr/include \

-I $ANDROID_HOME/external/curl/include/ \

-I $ANDROID_HOME/external/curl/3rd/include \

-I $ANDROID_HOME/external/curl \

-I $ANDROID_HOME/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates \

-I $ANDROID_HOME/dalvik/libnativehelper/include/nativehelper \

-I $ANDROID_HOME/system/core/include \

-I $ANDROID_HOME/hardware/libhardware/include \

-I $ANDROID_HOME/hardware/libhardware_legacy/include \

-I $ANDROID_HOME/hardware/ril/include \

-I $ANDROID_HOME/dalvik/libnativehelper/include \

-I $ANDROID_HOME/frameworks/base/include \

-I $ANDROID_HOME/frameworks/base/opengl/include \

-I $ANDROID_HOME/frameworks/base/native/include \

-I $ANDROID_HOME/external/skia/include \

-I $ANDROID_HOME/out/target/product/generic/obj/include \

-I $ANDROID_HOME/bionic/libc/arch-arm/include \

-I $ANDROID_HOME/bionic/libc/include \

-I $ANDROID_HOME/bionic/libstdc++/include \

-I $ANDROID_HOME/bionic/libc/kernel/common \

-I $ANDROID_HOME/bionic/libc/kernel/arch-arm \

-I $ANDROID_HOME/bionic/libm/include \

-I $ANDROID_HOME/bionic/libm/include/arch/arm \

-I $ANDROID_HOME/bionic/libthread_db/include \

-include $ANDROID_HOME/system/core/include/arch/linux-arm/AndroidConfig.h \

-I $ANDROID_HOME/system/core/include/arch/linux-arm/ \

-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \

CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \

-funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \

-fno-short-enums -march=armv5te -mtune=xscale -Wno-psabi -mthumb-interwork \

-fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \

-Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point \

-g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \

-fgcse-after-reload -frerun-cse-after-loop -frename-registers -UDEBUG \

-mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 \

-Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs \

-Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal \

-Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels \

-Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers" \

LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME/build/core/armelf.x \

-Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \

-L$ANDROID_HOME/out/target/product/generic/obj/lib -Wl,-z,noexecstack \

-Wl,-rpath-link=$ANDROID_HOME/out/target/product/generic/obj/lib \

-lc -llog -lcutils -lstdc++ \

-Wl,--no-undefined $ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a \

$ANDROID_HOME/out/target/product/generic/obj/lib/crtend_android.o \

-lm $ANDROID_HOME/out/target/product/generic/obj/lib/crtbegin_dynamic.o \

-L$ANDROID_HOME/external/curl/3rd/libs"3.编译libcurl.so库cd进入android/external/curl源码目录

mm-》编译生成libcurl.so库

4.编写测试case 以及Android.mk文件并生成可执行文件新建一个测试案例curl_test.cpp

include "curl/curl.h"

include ;

int main() {

CURL *curl;

CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();

if (curl) {

curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com/");

res = curl_easy_perform(curl);

if (0!=res) {

printf("curl error: %d\n", res);

}

curl_easy_cleanup(curl);

}

curl_global_cleanup();

return 0;

}在同一目录下写一个Android.mk文件生成curl_test可执行文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_C_INCLUDES += \

$(TOP)/external/curl/include/ \

LOCAL_SRC_FILES:= curl_test.cpp

No shared libraries.

No static libraries.

LOCAL_SHARED_LIBRARIES := libcurl

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := curl_test

include $(BUILD_EXECUTABLE)

生成可执行文件:curl_test

4.运行查看测试结果运行测试case:curl_test

5.(补充)移植libcurl到android4.0,修改两个地方1)把生成的路径改一下,一般默认为out/target/product/generic下面,我们根据系统不同(根据lunch选择不同,最终生成的路径不一样)改为系统的全局变量,

把$ANDROID_HOME/out/target/product/generic替换成$ANDROID_PRODUCT_OUT。

ANDROID_HOME_CURL=../../ && \

NDK_HOME_CURL=../../prebuilt/ndk && \

PATH="$ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \

./configure --host=arm-linux CC=gcc --with-random=/dev/urandom \

CPPFLAGS="-I$NDK_HOME_CURL/platforms/android-8/arch-arm/usr/include \

-I $ANDROID_HOME_CURL/external/curl/include/ \

-I $ANDROID_HOME_CURL/external/curl/3rd/include \

-I $ANDROID_HOME_CURL/external/curl \

-I $ANDROID_HOME_CURL/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates \

-I $ANDROID_HOME_CURL/dalvik/libnativehelper/include/nativehelper \

-I $ANDROID_HOME_CURL/system/core/include \

-I $ANDROID_HOME_CURL/hardware/libhardware/include \

-I $ANDROID_HOME_CURL/hardware/libhardware_legacy/include \

-I $ANDROID_HOME_CURL/hardware/ril/include \

-I $ANDROID_HOME_CURL/dalvik/libnativehelper/include \

-I $ANDROID_HOME_CURL/frameworks/base/include \

-I $ANDROID_HOME_CURL/frameworks/base/opengl/include \

-I $ANDROID_HOME_CURL/frameworks/base/native/include \

-I $ANDROID_HOME_CURL/external/skia/include \

-I $ANDROID_HOME_CURL/out/target/product/generic/obj/include \

-I $ANDROID_HOME_CURL/bionic/libc/arch-arm/include \

-I $ANDROID_HOME_CURL/bionic/libc/include \

-I $ANDROID_HOME_CURL/bionic/libstdc++/include \

-I $ANDROID_HOME_CURL/bionic/libc/kernel/common \

-I $ANDROID_HOME_CURL/bionic/libc/kernel/arch-arm \

-I $ANDROID_HOME_CURL/bionic/libm/include \

-I $ANDROID_HOME_CURL/bionic/libm/include/arch/arm \

-I $ANDROID_HOME_CURL/bionic/libthread_db/include \

-include $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/AndroidConfig.h \

-I $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/ \

-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \

CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \

-funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \

-fno-short-enums -march=armv5te -mtune=xscale -Wno-psabi -mthumb-interwork \

-fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \

-Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point \

-g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \

-fgcse-after-reload -frerun-cse-after-loop -frename-registers -UDEBUG \

-mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 \

-Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs \

-Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal \

-Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels \

-Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers" \

LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME_CURL/build/core/armelf.x \

-Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \

-L$ANDROID_PRODUCT_OUT/obj/lib -Wl,-z,noexecstack \

-Wl,-rpath-link=$ANDROID_PRODUCT_OUT/obj/lib \

-lc -llog -lcutils -lstdc++ \

-Wl,--no-undefined $ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a \

$ANDROID_PRODUCT_OUT/obj/lib/crtend_android.o \

-lm $ANDROID_PRODUCT_OUT/obj/lib/crtbegin_dynamic.o \

-L$ANDROID_HOME_CURL/external/curl/3rd/libs"2)修改Android.mk

ALL_PREBUILT += $(LOCAL_PATH)/NOTICE

$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)

$(copy-file-to-target)

把关于ALL_PREBUILT模块全部注释调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值