SwiftでiOSアプリケーションを作成しています。
rubyで実装した関数をSwiftファイルから呼び出したいです。
How to call C from Swift?
CアプリケーションからRubyスクリプトを呼び出す方法
以上の文献を参考にXcodeの設定や、Cファイル(作成したrubyファイルの関数を呼び出す)、rubyファイル(文字列をprint)を作成しました。
XcodeのTARGETS/Builds Setting/Apple Clang Custom Compiler Flagsの項目にgccで使うオプションは追加済みです。
(SwiftからCファイルの呼び出し、Cファイルからrubyファイルの呼び出しは、それぞれ動作確認済み)
一通り設定を変更したものを実行したところ以下のエラーに直面しました。
clang: error: linker command failed with exit code 1 (use -vto see invocation)
--追記--
Cruby cocopods
上記のgitからcocoadodsでCRubyを導入したのですがエラーで実行できず。
またcocoadodsで作成したlibCRuby_ios.aというios端末用のlibrubyをbuild Phases/Link binary with Librarysから追加したところbuildは成功。
しかし実行すると Thread 1: EXC_BAD_ACCESS (code=1, address=0xe0) で止まります。
以下に現在のコードを添付します。
C
1#include <stdio.h> 2#include "ruby.h" 3#include "ruby/encoding.h" 4 5VALUE $kernel; 6void init() 7{ 8 // Ruby初期化のおまじない 9 ruby_init(); 10 ruby_init_loadpath(); 11 rb_enc_find_index("encdb"); // encodingライブラリの初期化 12 int error; 13 rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "rubygems", &error); 14 rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./calee", &error); 15} 16 17void run() 18{ 19 VALUE module = rb_const_get(rb_cObject, rb_intern("Test"));//ここでエラー EXC_BAD_ACCESS 20 VALUE klass = rb_const_get(module, rb_intern("Callee")); 21 VALUE obj = rb_class_new_instance(0, NULL, klass); // Test::Callee.new 22 VALUE str = rb_str_new2("こんにちは"); 23 rb_funcall(obj, rb_intern("foo"), 1, str); // obj.foo(str) 24} 25 26void main() 27{ 28 init(); 29 run(); 30}
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 main() 9 } 10}
あなたの回答
tips
プレビュー