How to create an iOS / OSX library from Haxe and use it in your own application
Relevance: 12.2014; HXCPP-ver .: 3.1.39
~ git
.
Dependency: hxcpp
1. Haxe → Library
Create a new Haxe project with a 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
- Create a new Xcode project. It can be for OSX or iOS, Application or Cocoa Framework.
- In the "Project" / "Build Setting" / "Header Search Paths" settings add the paths to the dependencies: (all paths must be full / non-relative and recursive )
out/cpp/module/include
- you need to fix it to the full path;{your-haxelib-repo}/hxcpp/{version}/include
- {here-yours};/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
- In the settings "Project" / "Build Settings" / "Apple LLVM 6.0 - Language - C ++":
- 'C ++ language dialect' =
GNU++11 [-std=gnu++11]
- 'C ++ Standard Library' =
libstdc++ (GNU C++ standard library)
- In 'Project' / 'Build Phases' / 'Link Binary With Libraries':
- Rename file:
AppDelegate.m
→ AppDelegate.mm
- 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 autocompletion and better navigation, you can add reference groups from directories to the Xcode project:
include
from Haxe output;include
from haxelib hxcpp
.
Possible problems:
At a time when only one possible problem was written in this text. It can be solved by editing the file {haxelib:hxcpp}/include/hxcpp.h
. Just add a few lines at the beginning of the file:
{Haxelib: hxcpp} /include/hxcpp.h
#ifndef HXCPP_H #define HXCPP_H
see after // Standard headers ....
Sample project .
source share