Saturday, July 11, 2020

Haxe: Compile heaps.io game through hashlink/c code on Win10

Here is my setup
  • Windows 10
  • Visual Studio 2017
  • haxe 4.0.5
  • hashlink 1.11 
  • heaps 1.8.0
note: cpp targeting is not currently supported by heaps
note: hashlink 1.11 comes with x64 libraries, so we'll be compiling our game for x64. If you want to target x86, you will likely need to compile hashlink yourself to get the x86 libs for linking.

Main.hx -- this is the hello world sample from heaps.io
class Main extends hxd.App {
    override function init() {
        var tf = new h2d.Text(hxd.res.DefaultFont.get(), s2d);
        tf.text = "Hello World !";
    }
    static function main() {
        new Main();
    }
}

build.hxml
-lib heaps
-lib hldx
-hl out/main.c
-main Main


Compile it into c code
haxe build.hxml


We will use Visual Studio to compile the c code
  1. start menu > x64 Native Tools Command Prompt for VS
    1. alternatively, open cmd and run vcvars64.bat (since we are targeting x64)
      for VS 2017, it should be located at C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat
  2. cd "path to your project directory"
  3. cl /Ox /Fo:main.obj /Fe:main.exe out/main.c /I out /I C:/HaxeToolkit/hl-1.11.0-win/include /link /LIBPATH:C:/HaxeToolkit/hl-1.11.0-win libhl.lib ssl.lib ui.lib directx.lib openal.lib fmt.lib
If everything goes well, a main.exe would be created in your project's directory. This is what the output looks like
C:\my_game>cl /Ox /Fo:main.obj /Fe:main.exe out/main.c /I out /I C:/HaxeToolkit/hl-1.11.0-win/include /link /LIBPATH:C:/HaxeToolkit/hl-1.11.0-win libhl.lib ssl.lib ui.lib directx.lib openal.lib fmt.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27035 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

main.c
Microsoft (R) Incremental Linker Version 14.16.27035.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
/LIBPATH:C:/HaxeToolkit/hl-1.11.0-win
libhl.lib
ssl.lib
ui.lib
directx.lib
openal.lib
fmt.lib
main.obj

C:\my_game>


If you want to hide the console window, call hl.UI.closeConsole() at startup as ncannasse suggested.


Ref:
How to compile c files generated by HashLink
Error on Compile C generated files using CL.EXE
How to compile x64 code with Visual Studio in command line?
How to resolve the module machine type 'X86' conflicts with target machine type 'x64' Visual Studio
HashLink/C Compilation - Haxe - The Cross-platform Toolkit
Getting started with Haxe/C++ - Haxe - The Cross-platform Toolkit
MSVC compiler options
MSVC linker options 
[SOLVED] Error when compiling heaps to cpp Windows - Heaps.io

No comments: