如何从Haxe创建iOS-和OSX-库并在本机应用程序中使用它?

Ale*_*oz. 7 macos native haxe hxcpp ios

我有一个在Haxe上编写自己的协议,数据结构和逻辑的跨平台实现.如何在iOS和OSX的企业应用程序(使用本机UI)中构建和使用它?

Ale*_*oz. 9

如何从Haxe创建iOS-/OSX-库并在本机应用程序中使用它

现状:12.2014; HXCPP-VER: 〜.3.1.39git

相关性: hxcpp

1. Haxe - >图书馆

使用名为的主类创建一个新的Haxe项目HxModule.

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

    @:headerCode
    public static function test():Int
    {
        return 101;
    }
}
Run Code Online (Sandbox Code Playgroud) 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
Run Code Online (Sandbox Code Playgroud)

建立: $ haxe buid.hxml

2. Xcode-project < - Library

  1. 创建一个新的Xcode项目.它可以用于OSX或iOS,应用程序或Cocoa Framework.
  2. 在"项目"/"构建设置"/"标题搜索路径"中添加依赖项的路径:(所有路径必须是完整/非相对递归)
    1. out/cpp/module/include- 你必须将它修复到完整的路径 ;
    2. {your-haxelib-repo}/hxcpp/{version}/include - {here-yours};
    3. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
  3. 在"项目"/"构建设置"/"Apple LLVM 6.0 - 语言 - C++"中更改值:
    • 'C++语言方言'= GNU++11 [-std=gnu++11]
    • 'C++标准库'= libstdc++ (GNU C++ standard library)
  4. 在'Project'/'Build Phases'/'Link Binary With Libraries'中:
    • HxModule.a
  5. 重命名文件:AppDelegate.m- >AppDelegate.mm
  6. 编辑AppDelegate.mm:
AppDelegate.mm
#import "AppDelegate.h"
#import "HxModule.h"

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

此外,对于自动完成和更好的导航,您可以在目录中添加Xcode-project 参考组:

  • include 来自Haxe的输出;
  • include来自haxelib hxcpp.
可能的问题:

在撰写本文时,只有一个可能的问题.它可以通过编辑文件来解决{haxelib:hxcpp}/include/hxcpp.h.只需在文件的开头添加几行:

{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; }
...
Run Code Online (Sandbox Code Playgroud)

看后// Standard headers .....

示例项目.