COMMUNITY

Using a Haxe library compiled to C++, from C++

haxe-cpp

(Endel Dreyer) #1

Hi everyone,

I’ve followed the nice tip from @dmitryhryppa, and managed to compile my Haxe library to C++. I’m having some questions on how to use the library now from C++, though.

The library I’m compiling to C++ is Colyseus, and I’d like to integrate it in a Cocos2D-X project. So far, apparently, I’ve linked and compiled correctly, but when I try to instantiate my Haxe classes, I always get an error.

Here’s what I’ve tried:

#include "io/colyseus/Client.h" /* import to my hxcpp code */
#include "io/colyseus/Room.h"   /* import to my hxcpp code */

USING_NS_CC; /* cocos2d-x thing */

io::colyseus::Client* colyseus;  /* declare pointer[?] to my hxcpp class */
io::colyseus::Room room;

Scene* HelloWorld::createScene()
{
    // trying to use my hxcpp library
    colyseus = new io::colyseus::Client("ws://localhost:2657");
    room = colyseus->GetPtr()->join("chat", nullptr);

    // cocos2d-x thing
    return HelloWorld::create();
}

With this piece of code, here’s what the C++ compiler says to me:

/usr/local/lib/haxe/lib/hxcpp/4,0,4/include/hx/Object.h:358:18: error: cannot initialize a parameter of type 'hx::Object *' with an rvalue of type 'char *'
         CastPtr(const_cast<SOURCE_ *>(inPtr),inCheckCast);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/endel/Projects/colyseus-cpp/cocos2dx/Classes/HelloWorldScene.cpp:38:20: note: in instantiation of function template specialization
      'hx::ObjectPtr<io::colyseus::Client_obj>::ObjectPtr<char>' requested here
    colyseus = new io::colyseus::Client("ws://localhost:2657");
                   ^
In file included from /Users/endel/Projects/colyseus-cpp/cocos2dx/Classes/HelloWorldScene.cpp:28:
In file included from /Users/endel/Projects/colyseus-hx/build/cpp/include/io/colyseus/Client.h:6:
In file included from /usr/local/lib/haxe/lib/hxcpp/4,0,4/include/hxcpp.h:338:
/usr/local/lib/haxe/lib/hxcpp/4,0,4/include/hx/Object.h:306:36: note: passing argument to parameter 'inPtr' here
   inline void CastPtr(hx::Object *inPtr,bool inThrowOnInvalid)
                                   ^
3 warnings and 2 errors generated.

** BUILD FAILED **

Any help is appreciated! I look forward to hear from you!

Cheers,
Endel


(Alan) #2

Hello @endel,

I’m no expert here but it seems like the error message is pointing you to convert the “string”/char* argument to the io::colyseus::Client constructor to an ObjectPtr<char> type instead…


(Kevin Leung) #3

You can try writing the same code in Haxe and compile to hxcpp. Then you investigate what the “correct” usage is from the generated code.