EDIT
The problem was caused by wrongly using @:extern("Greeter")
instead of @:native("Greeter*")
in the Greeter.hx extern file. Thanks Aidan for pointing out!
EDIT END
I’m trying a very basic example using externs for .cpp files, and everything works fine except for the problem described further below:
// src/Greeter.cpp
#include <iostream>
class Greeter
{
public:
void sayHello () {
std::cout << "Greeter saying \"Hello!\" \n";
}
void sayGoodbye () {
std::cout << "Greeter saying \"Goodbye!\" " << std::endl;
}
};
// src/Greeter.hx
@:include("./Greeter.cpp")
@:extern("Greeter")
extern class Greeter {
@:native("new Greeter") public static function create():Greeter;
@:native("sayHello") public function sayHello():Void;
@:native("sayGoodbye") public function sayGoodbye():Void;
}
# build.hxml
-cp src
-D analyzer-optimize
-main Main
-cpp out
Compiling and running with the following Main.hx works fine, and gives the expected output…
// src/Main.hx
class Main {
static function main() {
final greeter:Greeter = Greeter.create();
// Uncommenting one of the lines below works fine
// Uncommenting both - Causes compile error!
greeter.sayHello();
//greeter.sayGoodbye();
}
}
The strange thing is that as soon as I uncomment BOTH of the greeter.sayHello()
and greeter.sayGoodbye()
lines, the program won’t compile. (It works fine with just any of them.)
Tried on both macOS and Windows, same problem.
Here’s the compiler output:
Compiling group: haxe
cl.exe -Iinclude -nologo /WX- /fp:precise -DHX_WINDOWS -GR -O2(optim-std) -Zi(debug) -Fdc:\Users\Cambiata MusikProd\Downloads\haxe-cpp-basic-example\out\obj/msvc19xp/vc.pdb(debug) -Od(debug) -O2(release) -Os(optim-size) -FS -Oy- -c -EHs
-GS- -arch:SSE2 -Ic:/Users/Cambiata MusikProd/Downloads/haxe-cpp-basic-example/.haxelib/hxcpp/4,2,1/include -DHXCPP_VISIT_ALLOCS(haxe) -DHX_SMART_STRINGS(haxe) -DHXCPP_API_LEVEL=400(haxe) -D_CRT_SECURE_NO_DEPRECATE -D_ALLOW_MSC_VER_MISMATCH -D_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH -wd4996 ... tags=[haxe,static]
- src/Main.cpp
Error: Main.cpp
./src/Main.cpp(32): error C2440: 'initializing': cannot convert from 'Greeter *' to 'Greeter'
./src/Main.cpp(32): note: No constructor could take the source type, or constructor overload resolution was ambiguous
./src/Main.cpp(33): error C2819: type 'Greeter' does not have an overloaded member 'operator ->'
c:/Users/Cambiata MusikProd/Downloads/haxe-cpp-basic-example/src/Greeter.cpp(3): note: see declaration of 'Greeter'
./src/Main.cpp(33): note: did you intend to use '.' instead?
I’m on Haxe 4.2.5 and hxcpp 4.2.1.
Ideas?