首先贴下环境:Win7 64, NDK r8e, libiconv-1.14, cygwin
一 Win32环境配置
Cocos2D-X自带有win32上的iconv库,只需要配置一下即可使用。1 引入头文件
属性->配置属性->C/C++->附加包含目录:$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\iconv
2 引入静态库:libiconv.lib
属性->配置属性->连接器->输入->附加依赖项:libiconv.lib
3 定义一个公用转码函数
#include "iconv.h" bool iconv_convert(void *src, unsigned int src_len,char *src_charset, void *dest, unsigned int dest_len, char *dest_charset) { const char *in; char *out,*dest_ptr; size_t in_left,out_left,mutant,converted; in_left=src_len; out_left=dest_len; in=(char *)src; out=dest_ptr=(char *)dest; iconv_t oConv=iconv_open(dest_charset,src_charset); if(oConv==(iconv_t)(-1)) { CCLog("ERROR: unable to open libiconv."); return false; } mutant = iconv(oConv, &in, &in_left, &out, &out_left ); iconv_close(oConv); if(mutant == (size_t)(-1)) { CCLog("ERROR: unable to convert anything."); return false; } converted = dest_len - out_left; CCLog("to convert %u characters, %u mutanted , %u converted \n",src_len,mutant,converted); dest_ptr[converted]='\0'; return true; }
4 测试
void convertTest() { char inStr[] = "Hello, 这是个测试程序"; char outStr[1024]; iconv_convert(&inStr, sizeof(inStr), "GBK", &outStr, sizeof(outStr), "UTF-8"); CCLog("scr string:%s\n", inStr); CCLog("dst string:%s\n", outStr); }
输出结果:
to convert 22 characters, 0 mutanted , 29 converted
scr string:Hello, ????????????
dst string:Hello, 这是个测试程序
5 跨平台头文件包含设置
win32按如上方法,设置头文件包含路径和依赖库,然后才能#include "iconv.h";iOS上自带libiconv.dylib,只需#include <iconv.h>即可;
Android上需要另外编译,接下来我们会说;
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #include "iconv.h" #elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS #include <iconv.h> #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include "icov/iconv.h" #endif
二 为Android编译iconv
android下就不能直接使用cocos2d-x提供的iconv库,需要下载一个已经在linux环境下编译好的iconv库。1 下载
从官网下载libiconv-1.14.tar.gz包,重命名为libiconv,解压。官网下载地址:http://www.fsf.org/resources/
2 编译
打开cygwin,进入libiconv根目录,执行:./configure --build=x86_64-pc-linux-gnu --host=arm-linux-eabi
切记此处不是./configure,不然到后面会报一大堆stdio.h里面的错。
make
如果你之前make过了,需要先执行:
make clean
然后再执行make
3 拷贝头文件
将cocos2dx\platform\third_party\win32下的icov文件夹拷贝到项目的根目录下,跟Class同级4 配置
4.1 拷贝libiconv 到工程
将libiconv放置在cocos2d-x-2.1.4根目录下
4.2 编译配置
在libiconv目录下新建一个Android.mk文件,内容如下:
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := iconv_static LOCAL_MODULE_FILENAME := 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_STATIC_LIBRARY)
4.3 修改项目的Android.mk
a 添加搜索路径
将我们拷贝过来的iconv添加的头文件搜索路径中LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/iconv
b 添加iconv_static
LOCAL_WHOLE_STATIC_LIBRARIES += iconv_staticiconv_static是在iconv中的Android.mk中的LOCAL_MODULE的值
c 包含iconv子模块
导入我们在iconv中定义的Android.mk$(call import-module,libiconv)
三 常见错误
1 langinfo.h: No such file or directory
修改libiconv/libcharset/config.h文件中的宏定义:#define HAVE_LANGINFO_CODESET 1
为
#define HAVE_LANGINFO_CODESET 0
另:切记切记勿用百度,坑死,搜不到想要的;都做程序员了,还是想办法用谷歌吧。
2 'c' undeclared
修改文件/libcharset/lib/localcharset.c中函数get_charset_aliases (void),搜索int c;放到到函数体开头。
附录:
其他博客参考http://blog.csdn.net/smileyuping/article/details/9635365
http://xwrwc.blog.163.com/blog/static/4632000320138151017187/
http://blog.csdn.net/alex_my/article/details/10567541
http://blog.csdn.net/victoryckl/article/details/7089823