Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active August 6, 2017 17:18
Show Gist options
  • Save benolee/4511699 to your computer and use it in GitHub Desktop.
Save benolee/4511699 to your computer and use it in GitHub Desktop.
how easy it is to create and load a C extension in ruby

create a new dir

$ mkdir /tmp/ruby
$ cd /tmp/ruby

create extconf.rb

require 'mkmf'
create_makefile 'wat'

create c source

#include <ruby.h>
#include <stdio.h>

void
Init_wat(void)
{
  puts("hello, world!");
}

create makefile

$ ruby extconf.rb 
creating Makefile
$ ls
Makefile   extconf.rb wat.c

run make

$ make
compiling wat.c
linking shared-object wat.bundle
$ ls
Makefile   extconf.rb wat.bundle wat.c      wat.o

load extension

$ irb
>> require './wat'
hello, world!
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment