在用xcode写cocos2d的程序时,遇到一个问题,当调试环境是mac时,需要的文件路径为绝对路径,调试环境是iphone时,文件路径直接是文件名就可以了。为了解决这个问题,尝试写了一下宏。如下:
#define RUN_IN_IOS
#define TO_STRING(_string) #_string
#ifdef RUN_IN_IOS
#define GET_FILE(_fileName) TO_STRING(_fileName)
#else
#define GET_FILE(_fileName) TO_STRING(/Users/aaa/Resource/##_fileName)
#endif
调用的方式为:
string ttfPath = GET_FILE(Marker.ttf);
结果报错为:
Pasting formed '/Marker', an invalid preprocessing token
查询之后发现可能是clang编译的问题,但是我不太懂这个。
请教各位如果不使用这种写法,该怎么写达到我预计的效果?
谢谢各位!
#define TO_STRING(_string) #_string
#ifdef RUN_IN_IOS
试试
#define GET_FILE(_fileName) TO_STRING(_fileName)
#else
#define GET_FILE(_fileName) TO_STRING("/Users/aaa/Resource/"##_fileName)
#endif
调用的方式为:
string ttfPath = GET_FILE("Marker.ttf)";
不好意思,写错了,用下面的方式:
#define RUN_IN_IOS
#ifdef RUN_IN_IOS
#define GET_FILE(_fileName) _fileName
#else
#define GET_FILE(_fileName) "/Users/aaa/Resource/"##_fileName
#endif
调用的方式为:
char * pstr=GET_FILE("Marker.ttf");
谢谢各位已经解决了 最后写成了这样 虽然没有解决原来的疑惑 但总归是解决了问题
#ifdef RUN_IN_IOS
static string GET_FILE(string _fileName)
{
return _fileName;
}
#else
static string GET_FILE(string _fileName)
{
string path1 = "/Users/aaa/Resources/";
return path1+_fileName;
}
#endif
调用方法是
GET_FILE("Marker.ttf")
给字符串加上引号试试看。