cocos2dx添加第三方库注意事项

前一段时间,使用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);

你可能感兴趣的:(cocos2dx)