C++ 在Ruby 1.9.2中使用SWIG 2.0.4运行SWIG类示例
我只是尝试使用ruby示例来包装SWIG库包中的一个类。以下是我所做的:C++ 在Ruby 1.9.2中使用SWIG 2.0.4运行SWIG类示例,c++,ruby,dll,swig,C++,Ruby,Dll,Swig,我只是尝试使用ruby示例来包装SWIG库包中的一个类。以下是我所做的: 从以下位置下载了适用于mswin32的Ruby 1.9.2: 从此处下载SWIG 2.0.4: 在将项目文件转换为MS Visual Studio 2010并调整路径后,我设法用“example_wrap.cxx”文件编译了dll。它生成了一个很好的dll,只有一个导出的初始化函数。据我所知,这是完全正常的 我的问题是我不能使用附加的ruby脚本链接到dll并使用它。以下是ruby文件: # file: runme.r
- 从以下位置下载了适用于mswin32的Ruby 1.9.2:
- 从此处下载SWIG 2.0.4:
- 在将项目文件转换为MS Visual Studio 2010并调整路径后,我设法用“example_wrap.cxx”文件编译了dll。它生成了一个很好的dll,只有一个导出的初始化函数。据我所知,这是完全正常的
# file: runme.rb
# This file illustrates the C++ interface created by SWIG.
# All of our C++ classes get converted into Ruby classes.
require 'D:/Work/TUS_Work/temp/SWIG/class/example'
# ----- Object creation -----
print "Creating some objects:\n"
c = Example::Circle.new(10)
print " Created circle #{c}\n"
s = Example::Square.new(10)
print " Created square #{s}\n"
# ----- Access a static member -----
print "\nA total of #{Example::Shape.nshapes} shapes were created\n"
# ----- Member data access -----
# Set the location of the object
# Notice how we can do this using functions specific to
# the 'Circle' class.
c.x = 20
c.y = 30
# Now use the same functions in the base class
s.x = -10
s.y = 5
print "\nHere is their current position:\n"
print " Circle = (", c.x, ",", c.y, ")\n"
print " Square = (", s.x, ",", s.y, ")\n"
# ----- Call some methods -----
print "\nHere are some properties of the shapes:\n"
for o in [c, s]
print " #{o}\n"
print " area = ", o.area, "\n"
print " perimeter = ", o.perimeter, "\n"
end
# Notice how the Shape#area() and Shape#perimeter() functions really
# invoke the appropriate virtual method on each object.
print "\n", Example::Shape.nshapes," shapes remain\n"
print "Goodbye\n"
使用此文件作为参数运行ruby解释器后出现的错误是:
ruby.exe -I. runme.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load --
D:/Work/TUS_Work/temp/SWIG/class/example (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from runme.rb:6:in `<main>'
ruby.exe-I.runme.rb
:29:在“require”中:没有要加载的此类文件--
D:/Work/TUS_Work/temp/SWIG/class/example(加载错误)
from:29:in'require'
来自runme.rb:6:in`'
我在里面创建了一个example.rb文件,该文件已“找到”并加载,但即使我指定了完整路径和/或文件扩展名,也找不到dll
有没有其他人遇到过这个问题并找到了解决方案?我认为原因是ruby版本1.9.x可能已经改变了api,并失去了与旧代码的兼容性,因此这就是为什么这个简单示例不再工作的原因。但是我还没有找到这个问题的答案。Require没有完整的路径(或者至少它不应该这样使用)。试着这样做:
require 'example'
为了让Ruby找到文件,它会查找一些预定义的目录以及通过-I
命令行选项指定的目录。您可以将以下内容放在要求之前进行检查:
puts $LOAD_PATH
应该列出示例所在的目录,否则将找不到它。因此,请尝试以下方法:
ruby.exe -I. -ID:/Work/TUS_Work/temp/SWIG/class runme.rb
D:/Work/TUS_-Work/temp/SWIG/class/example.rb还是D:/Work/TUS_-Work/temp/SWIG/class/example.so存在?