haxiomic / haxe-c-bridge

Easily interact with haxe classes from C with an automatically generated C header
MIT License
51 stars 5 forks source link

Build errors when using README example #30

Closed skylerparr closed 2 years ago

skylerparr commented 3 years ago

Hello. This is a really cool project. This is something I really need. Unfortunately, I'm running into some build errors on the example and I'm currently stuck.

here's my setup: Windows 10

$ haxe --version
4.2.3

build.hxml

-cp src
-cp deps/haxe-c-bridge
-main Main
-cpp out
--dce full
-D dll_link

deps/haxe-c-bridge is a clone of master

hxcpp version:

  haxelib install hxcpp
You already have hxcpp version 4.2.1 installed

Main.hx code:

package;

class Main {
    public static function main():Void {
        trace("hello world");
    }
}

@:build(HaxeCBridge.expose())
class UseMeFromC {

    final callback: cpp.Callable<(num: Int) -> Void>;

    public function new(nativeCallback: cpp.Callable<(num: Int) -> Void>) {
        this.callback = nativeCallback;
    }

    public function add(a: Int, b: Int) {
        var result = a + b;
        callback(result);
        return result;
    }

    static public function exampleStaticFunction() {
        return "here's a string from haxe! In C this will be represented as a const char*. When passing haxe object to C, the object will be retained so it's not garbage collected while it's being used in C. When finished with haxe objects, you can call releaseHaxeString() or releaseHaxeObject()";
    }

}

My build error

Error: __HaxeCBridgeBindings__.cpp
./src/__HaxeCBridgeBindings__.cpp(30): error C2086: 'void *param': redefinition
./src/__HaxeCBridgeBindings__.cpp(33): error C2065: 'pthread_t': undeclared identifier
./src/__HaxeCBridgeBindings__.cpp(33): error C2146: syntax error: missing ')' before identifier 'handle'
./src/__HaxeCBridgeBindings__.cpp(33): error C2143: syntax error: missing ';' before '{'
./src/__HaxeCBridgeBindings__.cpp(33): error C2447: '{': missing function header (old-style formal list?)
./src/__HaxeCBridgeBindings__.cpp(150): error C7555: use of designated initializers requires at least '/std:c++latest'
./src/__HaxeCBridgeBindings__.cpp(200): error C2039: 'waitForThreadExit': is not a member of 'HaxeCBridgeInternal'
./src/__HaxeCBridgeBindings__.cpp(22): note: see declaration of 'HaxeCBridgeInternal'
./src/__HaxeCBridgeBindings__.cpp(200): error C3861: 'waitForThreadExit': identifier not found

Error: Build failed

Getting a build error on Linux too, but I'm less concerned about Linux not working, but here it is anyway:

Error: ./src/__HaxeCBridgeBindings__.cpp:38:37: error: cannot convert ‘std::nullptr_t’ to ‘pthread_t’ {aka ‘long unsigned int’} in initialization
   38 |  pthread_t haxeThreadNativeHandle = nullptr;
      |                                     ^~~~~~~
Error: Build failed

LMK if you have any clues. I'll be trying to debug on my end and if I find something I'll push up a PR.

Thanks again for the cool library.

skylerparr commented 3 years ago

I locally patched the HaxeCBridge.hx and got rid of the build errors. Testing the library to see if my changes worked.

haxiomic commented 3 years ago

Thanks for testing @skylerparr :), I've so far only tested on macOS with clang 11 (and there I get no errors) but it's useful to know about the errors on windows and linux: looks like there's some windows-specific thread code that I haven't taken care of properly! At a glance it looks like I'm missing some #ifdef windows around some non-windows thread code

With haxeThreadNativeHandle = nullptr, we're safe to remove the = nullptr

I'm away from my PC but over the weekend I'll run the unit test on windows

skylerparr commented 3 years ago

Still trying to verify windows. But, I was able to create a PR for the fix for Linux with a readme that describes some issues I had and how I solved them for my specific versions.

skylerparr commented 3 years ago

I created this PR that successfully builds for windows. But, the unit test still doesn't compile. On my first attempt I got:

.\app.c(7): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory

So I commented out the include, then got this build error:

.\app.c(176): error C2065: 'CLOCK_REALTIME': undeclared identifier
.\app.c(180): error C2065: 'CLOCK_REALTIME': undeclared identifier
.\app.c(212): error C2065: 'CLOCK_REALTIME': undeclared identifier
.\app.c(215): error C2065: 'CLOCK_REALTIME': undeclared identifier

So I defined the CLOCK_REALTIME as a constant to get around that error, so now I'm down to a more telling error:

app.obj : error LNK2019: unresolved external symbol _HaxeLib_initializeHaxeThread referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_stopHaxeThreadIfRunning referenced in function _onHaxeException
app.obj : error LNK2019: unresolved external symbol _HaxeLib_releaseHaxeString referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_releaseHaxeObject referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeNoArgsNoReturn referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_callInMainThread referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_callInExternalThread referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_add referenced in function _fnVoid
app.obj : error LNK2019: unresolved external symbol _HaxeLib_starPointers referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_rawPointers referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_hxcppPointers referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_hxcppCallbacks referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_externStruct referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_allocateABunchOfData referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_allocateABunchOfDataExternalThread referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_enumTypes referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_checkHaxeAnon referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_checkAnonFromPointer referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_createHaxeMap referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_checkHaxeMap referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_checkNull referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_createCustomType referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_createHaxeString referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_checkHaxeString referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_throwException referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Main_stopLoopingAfterTime_ms referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Main_getLoopCount referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Main_hxcppGcMemUsage referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Main_hxcppGcRun referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Main_printTime referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Instance_new referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Instance_methodNoArgs referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Instance_methodAdd referenced in function _main
app.obj : error LNK2019: unresolved external symbol _HaxeLib_Instance_overrideMe referenced in function _main
app.obj : error LNK2019: unresolved external symbol _sleep referenced in function _main
app.obj : error LNK2019: unresolved external symbol _clock_gettime referenced in function _main
app.exe : fatal error LNK1120: 38 unresolved externals

Which is a much more telling set of errors that looks like something still isn't right. My C++ isn't great, so there could be some other things I'm messing up. This is my build command I'm using from powershell:

 cl .\app.c /I .\haxe-bin\ .\haxe-bin\obj\lib\Main-debug.lib

I'm not 100% confident that this is the correct way to compile the program, so that could be the issue on its own. I hope this is useful for you to help get this working on Windows. LMK if there's anything else I can help with or look into to get this working.

haxiomic commented 3 years ago

Gotcha, this looks good :). The test code app.c is written against unixy libs for things like timestamping, not a big thing to swap to the windows alternatives there so that's easy to fix

The unresolved symbols error is saying that it's not linking against the generated library – I need to check docs to learn how cl.exe does things but I expect just a small tweak to compiler args

haxiomic commented 2 years ago

Should be good now :), try run.win.cmd for a working windows example