如何编译mruby例子吗? [英] How to compile an mruby example?

查看:263
本文介绍了如何编译mruby例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解 mruby 听证会启发我开始学习C语言编程。我走过了一些教程在网上工作,所以我了解的基础知识,但现在我想先通过编译一个示例应用程序与mruby播放。我知道如何编译一个C文件,但我被困在试图找出如何还用我自己的code编译一起mruby。

我用GCC在Mac OS X 10.8。

使用这个例子:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;#包括LT&;&mruby.h GT;
#包括LT&; mruby / compile.h>INT主要(无效)
{
  mrb_state * MRB = mrb_open();
  字符code [] =P世界,你好!'
  的printf(从C执行红宝石code \\ N!);  mrb_load_string(MRB,code);
  返回0;
}

和运行此命令:

 的gcc example.c

我明明得到错误:

  engine.c:4:19:错误:mruby.h:没有这样的文件或目录

我克隆了git的回购和创建符号链接到包含目录,但我pretty知道我做错了。

我应该怎样包括mruby在我的code,使这一切都编在一起?

更新:下面是我有什么目录结构,注意符号链接到mruby包括目录:

  $树-l。

├──example.c
└──包括
    └──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

当我通过包括目录编译:

 的gcc example.c -I包括/ mruby

我收到以下错误:

 适用于建筑x86_64的未定义符号:
  _mrb_load_string,从引用:
      在_main ccd8XYkm.o
  _mrb_open,从引用:
      在_main ccd8XYkm.o
LD:符号(S)未找到x86_64的架构
collect2:劳工处返回1退出状态

更新:我也跟着在的说明mruby /安装文件(这基本上只是说运行make在mruby项目的根目录下)。这增加了 mruby /编译/主机目录下的一堆目录和领域,包括文件的lib / libmruby.a 。我能够通过编译时包括这个文件编译示例脚本。

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

现在我跑我的应用程序:

  $ ./a.out
从C执行红宝石code!
你好,世界!


解决方案

而不是

 的#include< mruby.h>
#包括LT&; mruby / compile.h>

尝试

 的#includemruby.h
#包括mruby / compile.h

让我知道,如果有什么差别。编译器使用不同的搜索路径,以&lt包括;>和。 是本地的。

您也可以尝试

  GCC -ipath /到/红宝石/有/ DIR example.c

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!"

解决方案

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

这篇关于如何编译mruby例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆