Ruby C扩展不能'require'd

时间:2022-10-30 23:17:32

I have a ruby extension that I'm building as a gem. It has this directory structure

我有一个ruby扩展,我正在建立一个宝石。它有这个目录结构

|-ext
  \-cowboy
    \- extconf.rb
    |- cowboy.c
|-lib
  \- cowboy.rb
  |- cowboy
    \- version.rb
|- test
  \- test_cowboy.rb

When I build it and install it, there are no errors, and 'require'ing the gem (e.g. require 'cowboy') works fine in irb.

当我构建并安装它时,没有错误,并且'要求'宝石(例如要求'牛仔')在irb中正常工作。

However, when I run "ruby test/test_cowboy.rb" I get a load error from the require (it actually is complaining about "require 'cowboy/cowboy'" that lives in lib/cowboy.rb.

但是,当我运行“ruby test / test_cowboy.rb”时,我从require中得到一个加载错误(它实际上是抱怨生活在lib / cowboy.rb中的“require'cowboy / cowboy'”。

Does anyone know why???

有谁知道为什么???

1 个解决方案

#1


3  

I suspect that:

我怀疑:

  1. in irb, when you require 'cowboy', that tells rubygems to set up the load paths automatically to point to the currently installed gem dir.
  2. 在irb中,当你需要'cowboy'时,告诉rubygems自动设置加载路径以指向当前安装的gem目录。
  3. when you run test/test_cowboy.rb it doesn't require 'cowboy'. This makes sense because during development, you don't want to load the installed version of the gem, which could be different from the code in your working dir.
  4. 当你运行test / test_cowboy.rb时,它不需要'cowboy'。这是有道理的,因为在开发过程中,您不希望加载已安装的gem版本,这可能与您工作目录中的代码不同。

I think you should create a test/test_helper.rb file that sets up the load path:

我认为你应该创建一个test / test_helper.rb文件来设置加载路径:

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

You may need to add other dirs if the compiled shared object file (.so or .bundle) isn't placed in lib.

如果编译的共享对象文件(.so或.bundle)未放在lib中,则可能需要添加其他目录。

Then in each test file (e.g. test/test_cowboy.rb), require test/test_helper.rb:

然后在每个测试文件中(例如test / test_cowboy.rb),需要test / test_helper.rb:

require File.expand_path('../test_helper.rb', __FILE__)

You'll need to adjust that relative path if you have subdirs. E.g. if you have a file test/shoes/spur.rb, you'd use:

如果你有子目录,你需要调整相对路径。例如。如果您有文件test / shoes / spur.rb,您可以使用:

require File.expand_path('../../test_helper.rb', __FILE__)

#1


3  

I suspect that:

我怀疑:

  1. in irb, when you require 'cowboy', that tells rubygems to set up the load paths automatically to point to the currently installed gem dir.
  2. 在irb中,当你需要'cowboy'时,告诉rubygems自动设置加载路径以指向当前安装的gem目录。
  3. when you run test/test_cowboy.rb it doesn't require 'cowboy'. This makes sense because during development, you don't want to load the installed version of the gem, which could be different from the code in your working dir.
  4. 当你运行test / test_cowboy.rb时,它不需要'cowboy'。这是有道理的,因为在开发过程中,您不希望加载已安装的gem版本,这可能与您工作目录中的代码不同。

I think you should create a test/test_helper.rb file that sets up the load path:

我认为你应该创建一个test / test_helper.rb文件来设置加载路径:

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

You may need to add other dirs if the compiled shared object file (.so or .bundle) isn't placed in lib.

如果编译的共享对象文件(.so或.bundle)未放在lib中,则可能需要添加其他目录。

Then in each test file (e.g. test/test_cowboy.rb), require test/test_helper.rb:

然后在每个测试文件中(例如test / test_cowboy.rb),需要test / test_helper.rb:

require File.expand_path('../test_helper.rb', __FILE__)

You'll need to adjust that relative path if you have subdirs. E.g. if you have a file test/shoes/spur.rb, you'd use:

如果你有子目录,你需要调整相对路径。例如。如果您有文件test / shoes / spur.rb,您可以使用:

require File.expand_path('../../test_helper.rb', __FILE__)