cocos2dx添加第三方库注意事项

本文介绍了在使用Cocos2d-x2.0开发过程中遇到的中文转码加载问题,并提供了通过在头文件附近加入特定预处理指令来解决该问题的方法。包括了一个自定义函数GBKToUTF8的实现,用于将GBK编码的字符串转换为UTF-8编码。
前一段时间,使用cocos2dx 2.0,在使用中文转码的时候,老是加载出问题。 

Error 1 error LNK2019: unresolved external symbol _libiconv_close referenced in function "public: int __thiscall HelloWorld::GBKToUTF8(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,char const *,char const *)" (?GBKToUTF8@HelloWorld@@QAEHAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBD1@Z) F:\cocos2d\cocos2d2\HelloWorld\proj.win32\HelloWorldScene.obj HelloWorld 
Error 2 error LNK2019: unresolved external symbol _libiconv referenced in function "public: int __thiscall HelloWorld::GBKToUTF8(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,char const *,char const *)" (?GBKToUTF8@HelloWorld@@QAEHAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBD1@Z) F:\cocos2d\cocos2d2\HelloWorld\proj.win32\HelloWorldScene.obj HelloWorld 
Error 3 error LNK2019: unresolved external symbol _libiconv_open referenced in function "public: int __thiscall HelloWorld::GBKToUTF8(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,char const *,char const *)" (?GBKToUTF8@HelloWorld@@QAEHAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBD1@Z) F:\cocos2d\cocos2d2\HelloWorld\proj.win32\HelloWorldScene.obj HelloWorld 


,原因是在引用第三方库的时候,只写了头文件,#include "platform\third_party\win32\iconv\iconv.h" 
只要在头文件附近加入 
#pragma comment(lib,"libiconv.lib") 

即: 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 
#include "platform\third_party\win32\iconv\iconv.h" 
#pragma comment(lib,"libiconv.lib") 
#endif 
可解决中文问题,下面附带函数: 

int HelloWorld::GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode) 



iconv_t iconvH;  iconvH = iconv_open(formCode,toCode); 

if(iconvH == 0)  

{   

return -1; 



const char* strChar = gbkStr.c_str(); 

const char** pin = &strChar; 

size_t strLength = gbkStr.length(); 

char* outbuf = (char*)malloc(strLength*4); 

char* pBuff = outbuf; 

memset(outbuf,0,strLength*4);  

size_t outLength = strLength*4; 

if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength)) 



iconv_close(iconvH);  

return -1; 



gbkStr = pBuff; 

iconv_close(iconvH);  

return 0; 



//----------------------------------------------------- 
std::string title = "啊看见电算会计快接啊但是"; 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 
GBKToUTF8(title,"gb2312","utf-8"); 
#endif 

CCLabelTTF* pLabel = CCLabelTTF::create(title.c_str(), "Thonburi", 24);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值