【cocos2d-x 3.x 学习与应用总结】4: 使用cocos compile编译apk

【cocos2d-x 3.x 学习与应用总结】4: 使用cocos compile编译apk,第1张

概述前言 本文总结了使用cocos compile命令编译cocos2d-x安卓apk的基本用法,以及记录一个使用NDK-r9d(gcc 4.8)编译C++11的hash_map遇到的一个问题: error: invalid use of incomplete type 'struct std::hash<MessageType>'。 cocos compile 基本用法 C++11中的强类型枚举(s 前言

本文总结了使用cocos compile命令编译cocos2d-x安卓apk的基本用法,以及记录一个使用NDK-r9d(gcc 4.8)编译C++11的hash_map遇到的一个问题: error: invalID use of incomplete type 'struct std::hash<MessageType>'

cocos compile 基本用法 C++11中的强类型枚举(scoped enum)作为hash表键值导致的编译错误

对于基本的数据类型已经有了预定义的hash函数:

// std::hash // defined in <funtional>template<> struct hash<bool>;template<> struct hash<char>;template<> struct hash<signed char>;template<> struct hash<unsigned char>;template<> struct hash<char16_t>;template<> struct hash<char32_t>;template<> struct hash<wchar_t>;template<> struct hash<short>;template<> struct hash<unsigned short>;template<> struct hash<int>;template<> struct hash<unsigned int>;template<> struct hash<long>;template<> struct hash<long long>;template<> struct hash<unsigned long>;template<> struct hash<unsigned long long>;template<> struct hash<float>;template<> struct hash<double>;template<> struct hash<long double>;template< class T > struct hash<T*>;

我的cocos代码中使用了一个强类型枚举作为键值的unordered_map,如下:

enum class MessageType{    kMessageTypeChangePage = 0,kMessageTypePushPage,kMessageTypePopPage,kMessageTypeChangeBackground,};class MessageCenter : public Singleton<MessageCenter> {    // ......private:    typedef std::vector<Message*>                           MessageQueue;    MessageQueue                                            messages_;    typedef std::set<PriorityHandler*>                      HandlerQueue;    typedef std::unordered_map<MessageType,HandlerQueue>   HandlerMap;    HandlerMap                                              handlerMap_;};

其中的HandlerMap类型就是以enum class MessageType来作为键值的hash表,在windows上我使用visual studio 2013编译项目,顺利通过。

编译apk时就出问题了。在使用cocos compile -p androID命令,使用NDK对上面的代码进行编译的时候,会发现如下错误:

In file included from D:/programs/androID-ndk-r9d/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/hashtable.h:35:0,from D:/programs/androID-ndk-r9d/sources/cxx-stl/gnu-libstdc++/4.8/include/unordered_map:47,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../renderer/CCTexture2D.h:32,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../base/CCProtocols.h:34,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../2d/CCNode.h:34,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../2d/CCScene.h:32,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../base/CCDirector.h:37,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:29,from D:\codes\3.9\PlayingWithCocos3D\proj.androID\../cocos2d/cocos/3d/../cocos2d.h:41,from jni/../../Classes/util/cocos_util.h:3,from jni/../../Classes/cocos_include.h:4,from jni/../../Classes/message/MessageCenter.h:4,from jni/../../Classes\message\MessageCenter.cpp:1:D:/programs/androID-ndk-r9d/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/hashtable_policy.h:1082:53: error: invalID use of incomplete type 'struct std::hash<MessageType>'       using __ebo_h1 = _Hashtable_ebo_helper<1,_H1>;                                                     ^

编译器抱怨说 : struct std::hash是不完整类型。它为什么会这样认为,这是因为C++标准中并没有要求对enum class类型的东西,编译器要为其提供默认的hash函数。windows上VC++编译器应该是提供了enum class的hash函数,因此编译能够通过。那么对于gcc编译器,要想解决这个问题就需要自己定义hash函数,针对我的enum class MessageType。

enum class MessageType{    kMessageTypeChangePage = 0,};namespace std {    template <>    struct hash<MessageType>    {        typedef MessageType     argument_type;        typedef size_t          return_type;        return_type operator() (const argument_type &arg) const        {            return static_cast<return_type>(arg);        }    };}

为了快点通过编译在androID上看到我的游戏测试结果,我定义了一个很简单的hash函数,直接把enum的值转为size_t。

有了这个hash函数,NDK编译通过了并且打包成功。

作者水平有限,对相关知识的理解和总结难免有错误,还望给予指正,非常感谢!

在这里也能看到这篇文章:github博客,CSDN博客,欢迎访问

@H_502_450@ 总结

以上是内存溢出为你收集整理的【cocos2d-x 3.x 学习与应用总结】4: 使用cocos compile编译apk全部内容,希望文章能够帮你解决【cocos2d-x 3.x 学习与应用总结】4: 使用cocos compile编译apk所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1077040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存