如何创建iOS-&来自Haxe的OSX-库并在本机应用程序中使用它?

时间:2014-12-07 14:31:21

标签: ios macos native haxe hxcpp

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

1 个答案:

答案 0 :(得分:9)

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

  

现状:12.2014; HXCPP-ver。:3.1.39git

     

依赖关系:hxcpp

1。 Haxe - >文库

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

的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

构建:$ 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 ++ Language Dialect'= 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
  7. AppDelegate.mm
    #import "AppDelegate.h"
    #import "HxModule.h"
    
    @implementation AppDelegate
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSLog(@"test: %d", ((int)HxModule_obj::test()));
    }
    @end
    

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

      来自Haxe输出的
    • include;
    • 来自haxelib include
    • 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; }
    ...
    

    查看// Standard headers ....

    Example project