How to create iOS- & OSX- library from Haxe and use it in native application?
Asked Answered
G

1

7

I have an crossplatform implementation of own protocol, data-structures and logic written on Haxe. How I can build and use it in my enterprise-application (with native UI) for iOS and OSX?

Glosso answered 7/12, 2014 at 14:31 Comment(0)
S
10

How to create iOS- / OSX- library from Haxe and use it in native application

Actuality: 12.2014; HXCPP-ver.: 3.1.39~git.

Dependency: hxcpp

1. Haxe -> Library

Create a new Haxe-project with main class named HxModule.

src/HxModule.hx
class HxModule
{
    public static function main()
    {
        Sys.println('Hello from HxModule: "${test()}"');
    }

    @:headerCode
    public static function test():Int
    {
        return 101;
    }
}
build.hxml
-main HxModule
-cp src

-lib hxcpp

# this is for Mac OS X:
-D HXCPP_M64

# this is required on Windows. the "d" stands for debug:
#-D ABI=-MTd
--each

# at this phase we create a binary for tests
-cpp out/cpp/module


--next
# at this phase we create a binary for tests
-cpp out/cpp/module

-D static_link
-D actuate

Build: $ haxe buid.hxml

2. Xcode-project <- Library

  1. Create a new Xcode-project. It can be for OSX or iOS, Application or Cocoa Framework.
  2. In the 'Project' / 'Build Setting' / 'Header Search Paths' add paths to dependencies: (all paths must be full/not-relative and recursive)
    1. out/cpp/module/include - you have to fix it to full path;
    2. {your-haxelib-repo}/hxcpp/{version}/include - {here-yours};
    3. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
  3. In the 'Project' / 'Build Settings' / 'Apple LLVM 6.0 - Language - C++' change values:
    • 'C++ Language Dialect' = GNU++11 [-std=gnu++11]
    • 'C++ Standard Library' = libstdc++ (GNU C++ standard library)
  4. In the 'Project' / 'Build Phases' / 'Link Binary With Libraries':
    • HxModule.a
  5. Rename file: AppDelegate.m -> AppDelegate.mm
  6. Edit AppDelegate.mm:
AppDelegate.mm
#import "AppDelegate.h"
#import "HxModule.h"

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSLog(@"test: %d", ((int)HxModule_obj::test()));
}
@end

Additionally for autocomplete and better navigation you can add into Xcode-project the reference groups from directories:

  • include from output of Haxe;
  • include from haxelib hxcpp.
Possible problems:

At the time when this text was written only one possible issue. It can be solved by editing the file {haxelib:hxcpp}/include/hxcpp.h. Simply add a few lines at the beginning of the file:

{haxelib:hxcpp}/include/hxcpp.h
#ifndef HXCPP_H
#define HXCPP_H

// Standard headers ....

// Custom override by @suhinini
#define Class HxcppClass

// Basic mapping from haxe -> c++

typedef int Int;
typedef bool Bool;


// Windows hack
#define NOMINMAX


#ifdef _MSC_VER
   #include <typeinfo.h>
   namespace hx { typedef ::type_info type_info; }
...

see after // Standard headers .....

Example project.

Spiffing answered 7/12, 2014 at 14:31 Comment(4)
I have this setup working until I use the new operator to create an internal haxe object. e.g. @:headerCode public static function testSimple() { var simple = new Simple(); }Badge
@TomLarkworthy, this solution work fine (but with dirty hacks in hxcpp) only before Haxe 3.2 RC.1. Very soon I'll try to update this note.Glosso
I managed to get objects created within the Haxe env with ``` HX_TOP_OF_STACK hx::Boot(); __boot_all(); ``` before calling into the Haxe libraryBadge
I have this setup working for MAC but its not working for iphone build. For ios i am encountering the issue of reference. "ld: warning: ignoring file HaxeToObjC/libTestJSiOS.a, file was built for archive which is not the architecture being linked (i386): HaxeToObjC/libTestJSiOS.a Undefined symbols for architecture i386: TestJSiOS_obj::test()", referenced from: -[AppDelegate application:didFinishLaunchingWithOptions:] in AppDelegate.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)Tideway

© 2022 - 2024 — McMap. All rights reserved.