概要
RubyからC++のコードをよびだしたいので、Ruby-FFIを導入しようとしています。
bundlerからffi (1.17.0 ecfb225)がインストール済みなことは確認できるんですが、requireできず困っています。
インタプリタも静的解析もrequireができないとエラーを表示しています。
solagraph said: Required path ffi could not be resolved.RequireNotFound
interpreter said: `require': cannot load such file -- ffi (LoadError)
Gemfileは以下です。
# frozen_string_literal: true
source "https://rubygems.org"
# Specify your gem's dependencies in asciiartace.gemspec
gemspec
gem "rake", "~> 13.0"
gem "ffi", github: "ffi/ffi", submodules: true
group :development do
gem "rubocop", require: false
gem "rubocop-performance", require: false
gem "solargraph", require: false
end
ソースは以下で、
# frozen_string_literal: true
require_relative "test/version"
require "ffi"
# processing
module test
class Error < StandardError; end
extend FFI::Library
ffi_lib "libhello.so"
atach_function :add, %i[int int], :int
end
puts Asciiartace.add(2, 4)
cppコードは以下。
#include <iostream>
extern "C"
{
int add(int a, int b)
{
return a + b;
}
}
構築手順
$ bundle gem test
- edit gemfile
$ bundle install
- setup rspec
構造
.
|-- Gemfile
|-- Gemfile.lock
|-- README.md
|-- Rakefile
|-- asciiartace.gemspec
|-- bin
| |-- console
| `-- setup
|-- lib
| |-- asciiartace
| |-- asciiartace.rb
| |-- lib.cpp
| `-- libhello.so
|-- sig
| `-- asciiartace.rbs
`-- vendor
`-- bundle
6 directories, 11 files
$ bundle show
Gems included by the bundle:
* asciiartace (0.1.0)
* ast (2.4.2)
* backport (1.2.0)
* benchmark (0.3.0)
* bundler (2.5.11)
* diff-lcs (1.5.1)
* e2mmap (0.1.0)
* ffi (1.17.0 ecfb225)
* jaro_winkler (1.6.0)
* json (2.7.2)
* kramdown (2.4.0)
* kramdown-parser-gfm (1.1.0)
* language_server-protocol (3.17.0.3)
* nokogiri (1.16.5)
* parallel (1.25.1)
* parser (3.3.3.0)
* racc (1.8.0)
* rainbow (3.1.1)
* rake (13.2.1)
* rbs (2.8.4)
* regexp_parser (2.9.2)
* reverse_markdown (2.1.1)
* rexml (3.3.0)
* rubocop (1.64.1)
* rubocop-ast (1.31.3)
* rubocop-performance (1.21.0)
* ruby-progressbar (1.13.0)
* solargraph (0.50.0)
* strscan (3.1.0)
* thor (1.3.1)
* tilt (2.3.0)
* unicode-display_width (2.5.0)
* yard (0.9.36)
補足
手順については、claude AIに教えてもらいました。
Q. I want to call C++ code from Ruby.
---
You can call C++ code from Ruby by using a Ruby binding or wrapper for C++. There are several ways to achieve this, but one popular approach is to use the Ruby FFI (Foreign Function Interface) library.
Here's an example of how you can use Ruby FFI to call a C++ function:
1. First, create a C++ file (e.g., `example.cpp`) with a function you want to call from Ruby:
```cpp
// example.cpp
#include <iostream>
extern "C" {
int add(int a, int b) {
return a + b;
}
}
```
Note: We use `extern "C"` to ensure that the C++ compiler doesn't mangle the function name, which is required for FFI to work correctly.
2. Compile the C++ code into a shared library (e.g., `example.so` on Linux/macOS or `example.dll` on Windows).
3. In your Ruby script, use the FFI library to load the shared library and call the C++ function:
```ruby
require 'ffi'
module Example
extend FFI::Library
ffi_lib './example.so' # Replace with the path to your shared library
attach_function :add, [:int, :int], :int
end
puts Example.add(2, 3) # Output: 5
```
Here's what the Ruby code does:
- `require 'ffi'` loads the FFI library.
- `module Example` defines a Ruby module to hold the C++ function bindings.
- `extend FFI::Library` extends the module with the FFI::Library functionality.
- `ffi_lib './example.so'` specifies the path to the shared library containing the C++ code.
- `attach_function :add, [:int, :int], :int` defines the Ruby binding for the `add` function, specifying the argument types (two integers) and the return type (integer).
- `Example.add(2, 3)` calls the C++ `add` function through the Ruby binding.
Note that you need to have the FFI library installed. On most systems, it's available by default with Ruby, but you may need to install it separately on some platforms.
This example demonstrates a simple case of calling a C++ function from Ruby. You can extend this approach to call more complex C++ code, including classes, methods, and data structures, by using FFI's advanced features.