2

Hearing about mruby has inspired me to start learning C programming. I have worked through a few tutorials online so I understand the basics, but now I would like to start playing with mruby by compiling an example application. I understand how to compile a single C file, but I'm stuck at trying to figure out how to also compile mruby along with my own code.

I'm using GCC on Mac OS X 10.8.

Using this example:

#include <stdlib.h>
#include <stdio.h>

#include <mruby.h>
#include <mruby/compile.h>

int main(void)
{
  mrb_state *mrb = mrb_open();
  char code[] = "p 'hello world!'";
  printf("Executing Ruby code from C!\n");

  mrb_load_string(mrb, code);
  return 0;
}

And running this command:

gcc example.c

I obviously get the error:

engine.c:4:19: error: mruby.h: No such file or directory

I have cloned the git repo and created a symlink to the include directory, but I'm pretty sure I'm doing it wrong.

How should I include mruby in my code so that it's all compiled together?

Update: Here is the directory structure of what I have, note the symlink to the mruby include directory:

$ tree -l .
.
├── example.c
└── include
    └── mruby -> ../../mruby/include
        ├── mrbconf.h
        ├── mruby
        │   ├── array.h
        │   ├── class.h
        │   ├── compile.h
        │   ├── data.h
        │   ├── debug.h
        │   ├── dump.h
        │   ├── gc.h
        │   ├── hash.h
        │   ├── irep.h
        │   ├── khash.h
        │   ├── numeric.h
        │   ├── proc.h
        │   ├── range.h
        │   ├── string.h
        │   ├── value.h
        │   └── variable.h
        └── mruby.h

When I compile by including the directory:

gcc example.c -I include/mruby

I get the following errors:

Undefined symbols for architecture x86_64:
  "_mrb_load_string", referenced from:
      _main in ccd8XYkm.o
  "_mrb_open", referenced from:
      _main in ccd8XYkm.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Update: I followed the instructions in the mruby/INSTALL file (which basically just said run make in the root directory of the mruby project). This added a bunch of directories and field in the mruby/build/host directory, including the file lib/libmruby.a. I was able to compile the example script by including this file when compiling.

gcc -Iinclude/mruby example.c ../mruby/build/host/lib/libmruby.a

Now I run my app:

$ ./a.out
Executing Ruby code from C!
"hello world!"
Andrew
  • 227,796
  • 193
  • 515
  • 708

1 Answers1

1

Instead of

#include <mruby.h>
#include <mruby/compile.h>

Try

#include "mruby.h"
#include "mruby/compile.h"

Let me know if that makes any difference. The compiler uses different search paths for includes with <> and "". "" are local.

You can also try

gcc -Ipath/to/ruby/include/dir example.c
Charlie Burns
  • 6,994
  • 20
  • 29
  • changing to double quotes didn't change anything. The real error that I'm getting now is `Undefined symbols for architecture x86_64`, I've updated my question to provide more detail. – Andrew Oct 12 '13 at 03:03
  • I don't see an updated question. Did the "mruby.h: No such file or directory" go away and get replaced by the "Undefined symbols for architecture x86_64" error? – Charlie Burns Oct 12 '13 at 03:06
  • Sorry, just finished posting the update, check again – Andrew Oct 12 '13 at 03:06
  • Following the example you gave of using the `-I` flag and passing the directory with the header files results in the error mentioned above. – Andrew Oct 12 '13 at 03:07
  • Ok, you are making progress. gcc found ruby.h. What you to do now is to link with the mruby library. Did mruby come with source code? In addition to the header files, you need compile with a library that mruby provides ( this is a binary file ) or compile mruby itself. – Charlie Burns Oct 12 '13 at 03:08
  • I thought the header files were source code. What should I be looking for? Here is the repo I cloned: https://github.com/mruby/mruby – Andrew Oct 12 '13 at 03:09
  • Did you read and follow the INSTALL file in that repo? If so, somewhere, probably in lib, is a file named libmruby.a or something like that. You need to add file to your gcc line like "gcc -Ipath/to/ruby/include/dir example.c path/to/libruby.a" – Charlie Burns Oct 12 '13 at 03:13
  • Ah, I did not see that file in there. Also, I did not know that I needed to "precompile" mruby since I was going to be compiling it with my own files. I will go through the INSTALL doc and see what changes. – Andrew Oct 12 '13 at 03:37
  • After make succeeds look in build/host/lib for the .a files. Add one or both of those .a files to your gcc command line. If gcc succeeds, it will create an executable named "a.out". You can run that with "./a.out". Good luck. I'm going to bed. – Charlie Burns Oct 12 '13 at 03:46
  • After running make in the root dir, a bunch of files are added to the `build/host` directory, but the only file in the `build/host/lib` directory is a file called `libmruby.flags.mak` – Andrew Oct 12 '13 at 04:11
  • Nevermind! For some reason Sublime Text 2 was not displaying the `.a` files. I was able to compile the code once I included the `.a` file. Thanks for your help! – Andrew Oct 12 '13 at 04:33