COMMUNITY

@:nativeGen corrupts generated cpp code

I want to compile a few haxe classes as a cpp library. The library contains only interfaces and classes implementing them. Since the library is going to be used by third-party integrators ignoring haxe heritage, types are decorated with @:nativeGen metadata.

As an example, consider the following code:

//MyInterface.hx
package foo;

@:nativeGen
interface MyInterface {
  function bar(): Void;
}
//MyClass.hx
package foo;

@:nativeGen
class MyClass implements MyInterface {
  public function new() {}

  public function bar() {
    trace("bar");
  }
}

When I compile the code above with the command haxe foo.MyInterface foo.MyClass -cpp cpp, the compiler complains that

Error: In file included from ./src/foo/MyClass.cpp:5:
include/foo/MyClass.h:23:38: error: expected unqualified-id
class HXCPP_CLASS_ATTRIBUTES MyClass , public virtual foo::MyInterface
^
./src/foo/MyClass.cpp:15:6: error: incomplete type ‘foo::MyClass’ named in nested name specifier
void MyClass::bar(){
^~~~~~~~~
include/foo/MyClass.h:23:30: note: forward declaration of ‘foo::MyClass’
class HXCPP_CLASS_ATTRIBUTES MyClass , public virtual foo::MyInterface
^
./src/foo/MyClass.cpp:22:1: error: incomplete type ‘foo::MyClass’ named in nested name specifier
MyClass::MyClass(){
^~~~~~~~~
include/foo/MyClass.h:23:30: note: forward declaration of ‘foo::MyClass’
class HXCPP_CLASS_ATTRIBUTES MyClass , public virtual foo::MyInterface

The problem is in the malformed header MyClass.h.

//excerpt from MyClass.h
//NB the comma after MyClass should be a colon
class HXCPP_CLASS_ATTRIBUTES MyClass , public virtual foo::MyInterface
{
	public:
		MyClass();

		void bar();

};
//excerpt from MyInterface.h
class HXCPP_CLASS_ATTRIBUTES MyInterface : public virtual ::hx::NativeInterface
{
	public:
		virtual void bar( )=0;
};

What is going wrong?
And why does the interface extend ::hx::NativeInterface? Is it possible to avoid the pollution of ::hx::NativeInterface?

Unfortunately, @:nativeGen will not work for classes. Check this discussion: @:nativeGen half working on cpp · Issue #7199 · HaxeFoundation/haxe · GitHub

So, you need to remove @:nativeGen on MyClass and keep it on your interface only.

Thank you dmitryhryppa.

In your opinion, is hxcpp mature enough to build libraries geared towards third-party users?

Glad to help!

Oh, I can’t say exactly :slight_smile:
But I don’t think there are many (or at least one) real projects who are making external libs with hxcpp in production. So you may face some unknown issues. But generally speaking, it should work, and hxcpp itself is pretty mature.

Also, don’t forget that Haxe has GC, so you need to deal with it too.
You could check hxcpp tests to find how to use some undocumented things (extern-lib and extern-use folders may be useful for you):

And here is some useful docs:


Well, if you just want to make a C++ lib, probably hxcpp is not the best way. But if you have a Haxe codebase and you want to share it between different platforms (like JVM, Web, C++, etc), then that may be an option.

Another option that could be useful is haxe-c-bridge, which generates a C interface for your library

It takes care of the GC and thread-safety by running haxe in a separate thread and synchronising when calling into haxe code

Thank you so much, I really appreciate your help.