level 9
sevk
楼主
用C写自己的ruby类库 下载ruby源码: linux: apt-get install ruby1.9.1-dev .最简单.
windows: http://www.ruby-lang.org/zh_cn/downloads/ , 添加 include 和 lib 的环境变量.
编辑MyTest/MyTest.c :
C代码
// Include the Ruby headers and goodies
#include "ruby.h"
// Defining a space for information and references about the module to be stored internally
VALUE MyTest = Qnil;
// Prototype for the initialization method - Ruby calls this, not you
void Init_mytest();
// Prototype for our method 'test1' - methods are prefixed by 'method_' here
VALUE method_test1(VALUE self);
// The initialization method for this module
void Init_mytest() {
MyTest = rb_define_module("MyTest");
rb_define_method(MyTest, "test1", method_test1, 0);
}
// Our 'test1' method.. it simply returns a value of '10' for now.
VALUE method_test1(VALUE self) {
int x = 10;
return INT2NUM(x);
}
编辑 MyTest/extconf.rb :
Ruby代码
# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'
# Give it a name
extension_name = 'mytest'
# The destination
dir_config(extension_name)
# Do the work
create_makefile(extension_name)
运行 kk@ubuntu:~/MyTest$ ruby extconf.rb
creating Makefile
运行 kk@ubuntu:~/MyTest$ make
gcc -I. -I/usr/include/ruby-1.9.1/i486-linux -I/usr/include/ruby-1.9.1/ruby/backward -I/usr/include/ruby-1.9.1 -I. -D_FILE_OFFSET_BITS=64 -fPIC -fno-strict-aliasing -g -g -O2 -O2 -g -Wall -Wno-parentheses -fPIC -o test.o -c test.c
gcc -shared -o mytest.so test.o -L. -L/usr/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic -lruby-1.9.1 -lpthread -lrt -ldl -lcrypt -lm -lc
当前目录生成了 mytest.so 文件
这个 mytest.so 就可以用 ruby load 或 require 了.
Ruby代码
require 'mytest' ; include MyTest ; puts test1
输出 => 10
Ruby代码
# Load in the extension (on OS X this loads ./MyTest/mytest.bundle - unsure about Linux, possibly mytest.so)
require 'mytest'
# MyTest is now a module, so we need to include it
include MyTest
# Call and print the result from the test1 method
puts test1
# => 10
2010年06月10日 06点06分
1
windows: http://www.ruby-lang.org/zh_cn/downloads/ , 添加 include 和 lib 的环境变量.
编辑MyTest/MyTest.c :
C代码
// Include the Ruby headers and goodies
#include "ruby.h"
// Defining a space for information and references about the module to be stored internally
VALUE MyTest = Qnil;
// Prototype for the initialization method - Ruby calls this, not you
void Init_mytest();
// Prototype for our method 'test1' - methods are prefixed by 'method_' here
VALUE method_test1(VALUE self);
// The initialization method for this module
void Init_mytest() {
MyTest = rb_define_module("MyTest");
rb_define_method(MyTest, "test1", method_test1, 0);
}
// Our 'test1' method.. it simply returns a value of '10' for now.
VALUE method_test1(VALUE self) {
int x = 10;
return INT2NUM(x);
}
编辑 MyTest/extconf.rb :
Ruby代码
# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'
# Give it a name
extension_name = 'mytest'
# The destination
dir_config(extension_name)
# Do the work
create_makefile(extension_name)
运行 kk@ubuntu:~/MyTest$ ruby extconf.rb
creating Makefile
运行 kk@ubuntu:~/MyTest$ make
gcc -I. -I/usr/include/ruby-1.9.1/i486-linux -I/usr/include/ruby-1.9.1/ruby/backward -I/usr/include/ruby-1.9.1 -I. -D_FILE_OFFSET_BITS=64 -fPIC -fno-strict-aliasing -g -g -O2 -O2 -g -Wall -Wno-parentheses -fPIC -o test.o -c test.c
gcc -shared -o mytest.so test.o -L. -L/usr/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic -lruby-1.9.1 -lpthread -lrt -ldl -lcrypt -lm -lc
当前目录生成了 mytest.so 文件
这个 mytest.so 就可以用 ruby load 或 require 了.
Ruby代码
require 'mytest' ; include MyTest ; puts test1
输出 => 10
Ruby代码
# Load in the extension (on OS X this loads ./MyTest/mytest.bundle - unsure about Linux, possibly mytest.so)
require 'mytest'
# MyTest is now a module, so we need to include it
include MyTest
# Call and print the result from the test1 method
puts test1
# => 10