加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 16:52:16 所属栏目:百科 来源:网络整理
导读:前言 本文总结了使用 cocos compile 命令编译cocos2d-x安卓apk的基本用法,以及记录一个使用NDK-r9d(gcc 4.8)编译C++11的 hash_map 遇到的一个问题: error: invalid use of incomplete type 'struct std::hashMessageType' 。 cocos compile 基本用法 C++11

前言

本文总结了使用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:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../renderer/CCTexture2D.h:32,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../base/CCProtocols.h:34,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../2d/CCNode.h:34,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../2d/CCScene.h:32,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../base/CCDirector.h:37,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:29,from D:codes3.9PlayingWithCocos3Dproj.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/../../ClassesmessageMessageCenter.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博客,欢迎访问

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读