c - 如何编译 ruby​​ 示例?

标签 c ruby compilation mruby

听说 mruby启发了我开始学习 C 编程。我已经在线学习了一些教程,所以我了解了基础知识,但现在我想通过编译示例应用程序开始使用 mruby。我了解如何编译单个 C 文件,但我一直在尝试弄清楚如何将 mruby 与我自己的代码一起编译。

我在 Mac OS X 10.8 上使用 GCC。

使用这个例子:

#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;
}

然后运行这个命令:

gcc example.c

我显然得到了错误:

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

我已经克隆了 git 存储库并创建了指向包含目录的符号链接(symbolic link),但我很确定我做错了。

我应该如何在我的代码中包含 mruby 以便它们一起编译?

更新:这是我的目录结构,注意 mruby 包含目录的符号链接(symbolic link):

$ 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

当我通过包含目录进行编译时:

gcc example.c -I include/mruby

我收到以下错误:

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

更新:我按照mruby/INSTALL 文件中的说明进行操作(基本上只是说在mruby 项目的根目录中运行make)。这在 mruby/build/host 目录中添加了一堆目录和字段,包括文件 lib/libmruby.a。我能够通过在编译时包含此文件来编译示例脚本。

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

现在我运行我的应用程序:

$ ./a.out
Executing Ruby code from C!
"hello world!"

最佳答案

代替

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

尝试

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

让我知道这是否有任何不同。编译器使用不同的搜索路径来搜索包含 <> 和 ""的内容。 ""是本地的。

你也可以试试

gcc -Ipath/to/ruby/include/dir example.c

关于c - 如何编译 ruby​​ 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329146/

相关文章:

ruby - 重启 Sidekiq

scala - Intellij Idea无法识别Scala类中的可变字段,出现 'Reassignment to val'错误

php - 使用 GD 和 libjpeg 支持编译 PHP

c - for循环中的变量在同一循环中具有不同的值(C语言)

c - short int 和 unsigned short 的输出?

ruby - Heroku 应用程序崩溃并显示 'libruby.so.1.9: cannot open shared object file'

ruby - Ruby Enterprise 是否使用绿色线程?

c - 排序并从 c 中的 int 数组中删除重复项

c - GENERIC_ALL 和文件夹/文件 ACL? GENERIC_ALL 到底做了什么?

windows - 如何从命令行构建一个qt项目?