| 1 |
# Linking `libmruby` to your application |
| 2 |
|
| 3 |
You have two ways to link `libmruby` to your application. |
| 4 |
|
| 5 |
- using executable gem. |
| 6 |
- using normal compilation process |
| 7 |
|
| 8 |
## Executable Gems |
| 9 |
|
| 10 |
If your application is relatively small, `mrbgem` is an easier way to |
| 11 |
create the executable. By tradition, the gem name start with |
| 12 |
`mruby-bin-`, e.g. `mruby-bin-debugger`. |
| 13 |
|
| 14 |
### `mrbgem.rake` file |
| 15 |
|
| 16 |
The executable name is specified in `mrbgem.rake` file at the top of |
| 17 |
your `mrbgem` directory. |
| 18 |
|
| 19 |
```ruby |
| 20 |
MRuby::Gem::Specification.new('mruby-bin-example') do |spec| |
| 21 |
spec.license = 'MIT' |
| 22 |
spec.author = 'mruby developers' |
| 23 |
spec.summary = 'Example for executable command gem' |
| 24 |
spec.bins = %w(mruby-example) # <- this is binary name |
| 25 |
end |
| 26 |
``` |
| 27 |
|
| 28 |
### Source tree structure |
| 29 |
|
| 30 |
The source file for the gem executable should be in |
| 31 |
`<gem-name>/tools/<bin-name>`. Currently, we support C or C++ source code |
| 32 |
(`.c`, `.cpp`, `.cxx`, `.cc`) for the executable. Ruby source files are not |
| 33 |
supported. Put the functionality in the different gem and specify dependency to |
| 34 |
it in `mrbgem.rake`. |
| 35 |
|
| 36 |
## Normal compilation process |
| 37 |
|
| 38 |
The `libmruby` is a normal library so that you can just link it to your |
| 39 |
application. Specify proper compiler options (`-I` etc.) and linker options |
| 40 |
(`-lmruby` etc.) to compile and link your application. Specify those options in |
| 41 |
your build script (e.g. `Makefile`). |
| 42 |
|
| 43 |
### Compiler options |
| 44 |
|
| 45 |
You need to specify compiler options that are compatible to mruby configuration, |
| 46 |
for example: |
| 47 |
|
| 48 |
- `-I` to specify the place for mruby header files |
| 49 |
- `-D` to specify mruby configuration macros |
| 50 |
|
| 51 |
To retrieve compiler options used to build `mruby`, you can use `mruby-config` |
| 52 |
command with following options: |
| 53 |
|
| 54 |
- `--cc` compiler name |
| 55 |
- `--cflags` options passed to compiler |
| 56 |
|
| 57 |
```console |
| 58 |
$ mruby-config --cflags |
| 59 |
-std=gnu99 -g -O3 -Wall -DMRB_GC_FIXED_ARENA -I/home/matz/work/mruby/include -I/home/matz/work/mruby/build/host/include |
| 60 |
``` |
| 61 |
|
| 62 |
### Linker options |
| 63 |
|
| 64 |
Just like compiler options, you need to specify linker options that are |
| 65 |
compatible to mruby configuration. |
| 66 |
|
| 67 |
To retrieve linker options, you can use `mruby-config` with following options: |
| 68 |
|
| 69 |
- `--ld` linker name |
| 70 |
- `--ldflags` options passed to linker |
| 71 |
- `--ldflags-before-libs` options passed to linker before linked libraries |
| 72 |
- `--libs` linked libraries |
| 73 |
|
| 74 |
```console |
| 75 |
$ mruby-config --ldflags |
| 76 |
-L/home/matz/work/mruby/build/host/lib |
| 77 |
|
| 78 |
$ mruby-config --ldflags-before-libs |
| 79 |
# <nothing in this case> |
| 80 |
|
| 81 |
$ mruby-config --libs |
| 82 |
-lmruby -lm |
| 83 |
``` |