Linux环境下libiconv库基于Android NDK的编译方法

首先,到官网下载最新版的libiconv库:http://ftp.gnu.org/pub/gnu/libiconv/,我编译时为1.14版,libiconv-1.14.tar.gz,解压该文件到linux系统中指定目录,然后把其中的源文件全部放到jni文件中。首先在linux环境中进行配置,即首先对libiconv库执行configure,生产linux环境下的config.h文件,因为这样生产的config.h里面定义了很多linux平台下的宏,安装方法:首先,$./configure,然后$make,然后构建Android.mk文件放到jni目录中。
Android.mk文件内容:

LOCAL_PATH:= $(call my-dir)
#libiconv.so
include $
(CLEAR_VARS)
LOCAL_MODULE
:= libiconv
LOCAL_CFLAGS
:= \
 
-Wno-multichar \
 
-DANDROID \
 
-DLIBDIR="c" \
 
-DBUILDING_LIBICONV \
 
-DIN_LIBRARY

LOCAL_SRC_FILES
:= \
  libcharset
/lib/localcharset.c \
  lib
/iconv.c \
  lib
/relocatable.c

LOCAL_C_INCLUDES
+= \
  $
(LOCAL_PATH)/include \
  $
(LOCAL_PATH)/libcharset \
  $
(LOCAL_PATH)/lib \
  $
(LOCAL_PATH)/libcharset/include \
  $
(LOCAL_PATH)/srclib
include $
(BUILD_SHARED_LIBRARY)

#test
include $
(CLEAR_VARS)
LOCAL_MODULE
:=genutf8

LOCAL_SRC_FILES
:= tests/genutf8.c
LOCAL_C_INCLUDES
+= \
  $
(LOCAL_PATH)/include \
  $
(LOCAL_PATH)/libcharset \
  $
(LOCAL_PATH)/lib \
  $
(LOCAL_PATH)/libcharset/include \
  $
(LOCAL_PATH)/srclibLOCAL_SHARED_LIBRARIES := libiconv
include $
(BUILD_EXECUTABLE)

执行编译 $ndk-build

1、编译时提示:

libiconv-1.14/jni/libcharset/lib/localcharset.c:51:24: error: langinfo.h: No such file or directory

解决方法:

修改目录文件libcharset/config.h的宏定义,禁止该宏定义:

/* Define if you haveand nl_langinfo(CODESET). */

#define HAVE_LANGINFO_CODESET 0

2、提示错误:

报“c undeclaired”错误

解决方法:

修改文件/libcharset/lib/localcharset.c中函数get_charset_aliases (void)

搜索:int c;

把该变量定义放到该函数的开始处。

执行$ndk-build,编译libiconv.so成功。

到目录libs/armeabi-v7a下可以找到。

如果执行程序则需要打包到Android平台下。

相关推荐