使用extern从Haxe访问C ++类

Hel*_*ian 3 c++ haxe

我想从Haxe程序访问C ++代码。我正在尝试使用extern类,但遇到了一些问题。我正在尝试使用haxe -main Cpp_Extern -cpp Cpp_Extern进行编译。

我的Haxe代码:

@:include("./Rectangle.cpp")
@:extern("Rectangle*")
extern class Rectangle
{
    @:native("set_values") public function set_values(w : Int, h : Int) : Void;
    @:native("new Rectangle") public static function create() : Rectangle;
}

class Cpp_Extern
{
    public static function main()
    {
        Rectangle.set_values(10,20);
    }
}
Run Code Online (Sandbox Code Playgroud)

C ++代码

#include <iostream>

class Rectangle
{
  public:
  void set_values (int x, int y) 
    {
        std::cout << "x = " << x << "\n";
        std::cout << "y = " << y << "\n";
    }
};

int main()
{
    Rectangle one;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误是

C:\Users\ila5\Desktop\CPP\Cpp_Extern>haxe -main Cpp_Extern -cpp Cpp_Extern
haxelib run hxcpp Build.xml haxe -Dhaxe3="1" -Dhaxe_ver="3.201" -Dhxcpp_api_level="321" -I"C:\\HaxeToolkit\\haxe\\extraLibs/" -I"" -I"C:\\HaxeToolkit\\haxe\\std/cpp/_std/" -I"C:\\HaxeToolkit\\haxe\\std/"
cl.exe -Iinclude -nologo -O2 /WX- /fp:precise -DHX_WINDOWS -D_USING_V140_SDK71_ -GR -FS -Oy- -c -EHs -GS- -arch:SSE -IC:/HaxeToolkit/haxe/lib/hxcpp/3,2,205/include -DHXCPP_VISIT_ALLOCS -DHXCPP_API_LEVEL=321 -D_CRT_SECURE_NO_DEPRECATE -D_ALLOW_MSC_VER_MISMATCH -D_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH -wd4996 -MT ./src/Cpp_Extern.cpp -FoC:/Users/ila5/Desktop/CPP/Cpp_Extern/Cpp_Extern/obj/msvc19xp/2c1b12bd_Cpp_Extern.obj
Cpp_Extern.cpp
./src/Cpp_Extern.cpp(30): error C2440: 'initializing': cannot convert from 'Rectangle *' to 'Rectangle'
./src/Cpp_Extern.cpp(30): note: No constructor could take the source type, or constructor overload resolution was ambiguous
./src/Cpp_Extern.cpp(34): error C2819: type 'Rectangle' does not have an overloaded member 'operator ->'
C:/Users/ila5/Desktop/CPP/Cpp_Extern/Rectangle.cpp(4): note: see declaration of 'Rectangle'
./src/Cpp_Extern.cpp(34): note: did you intend to use '.' instead?
./src/Cpp_Extern.cpp(34): error C2232: '->Rectangle::set_values': left operand has 'class' type, use '.'

Error: Build failed
Run Code Online (Sandbox Code Playgroud)

tie*_*ery 5

您需要将@:include@:extern元数据添加到extern类中,以便hxcpp可以指向正确的文件并在编译时进行解析。

@:includemeta标签允许你指向一个文件,该文件放置在您的extern类定义的上方。该文件必须相对于您的构建输出目录。

@:externmeta标签告诉hxcpp类定义解析为您希望在C ++中看到类的名称。因此,如果要创建指针,则将使用new关键字实例化该类,并且extern定义如下:

@:include("includes/Rectangle.cpp")
@:extern("Rectangle*")
extern class Rectangle
{
    @:native("set_values") public function set_values(w : Int, h : Int) : Void;
    @:native("new Rectangle") public static function create() : Rectangle;
}
Run Code Online (Sandbox Code Playgroud)

您不能将new函数定义与extern类一起使用,因为它不是常规的Haxe类。相反,您需要制作一个执行实例化的静态函数,该函数会将extern类类型返回到Haxe上下文中,从而允许您访问其成员变量。

如果您遇到问题,包括要查找的hxcpp文件,则可以使用XML文件,该文件将用于告诉hxcpp要查找哪些文件夹。

在Windows上,您可以使用<files id="haxe">标签,后跟content <compilerflag value="-I/../includes/" />。当然,请关闭此标记,并确保includes文件夹位于项目的根目录中。

这将生成一个附加的编译器标志,通知Haxe编译器在附加文件夹中查找要包含的C ++源文件。然后include/,您@:include可以在extern类定义之前删除前缀。

然后,要告诉hxcpp使用XML文件,您需要使用@:buildXml主入口点上方的meta标记,该标记应包含以下内容:<include name="${haxelib:myCustomRepo}/../Build.xml" />同样,该标记必须相对于项目的根目录。

在上面的XML示例中,我使用,${haxelib:myCustomRepo}它告诉Haxe查找给定haxelib的路径。您可以使用以下命令设置haxelib存储库开发目录:haxelib dev myCustomRepo ./在这种情况下可能会有所帮助。这将完全取决于您是否使用该路由。

您不必使用XML文件,但是建议将其用于大型项目,尤其是在跨平台和目标构建时。

我还注意到您在C ++代码中缺少构造函数,因此请不要忘记将其放入。此外,您的主要入口点代码现在应如下所示:

@:buildXml('<include name="${haxelib:myCustomRepo}/../Build.xml" />') //only if you are using XML
class Main {
    public static function main() {
        var rect = Rectangle.create();
        rect.set_values(10, 20);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以通过两种方式在C ++中实例化对象。您可以使用new关键字,在这种情况下,返回的对象应该是指向该类型的指针。因此new Rectangle()将返回Rectangle*C ++。如果Rectangle.create()在Haxe中使用并将其分配给变量,则可以有效地得到此结果。

您也可以Rectangle rect;在C ++中实例化或声明。这就像结构一样被访问,因此->,您使用句号“” 代替使用访问成员。

为了在Haxe中工作,您需要@:structAccess在外部元素上添加元数据。此外,您需要更改@:native@:native("Rectangle*")@:native("Rectangle&")。这样可以确保将初始变量始终用作参考。

&与变量不同的是,使用变量作为参考的方法作为参考。

  • 好的,所以`cout`是`std`名称空间的一部分,因此您需要使用`std :: cout`。至于找不到“ set_values”,我相信这是因为您在C ++源文件中的定义末尾缺少分号。我可能是错的。当您解决第一个错误时,可能会自动解决其他错误。另外,在C ++中的Rectangle类下,创建一个构造函数:public:Rectangle();将允许根据需要创建对象。 (2认同)