我在 Ruby 环境中无法加载 C 扩展,只能加载纯 ruby 代码。我不能使用 RubyInline、FFI 或 Win32OLE,只有一种加载外部代码的方法:Win32API。但是我怎样才能编译出 ruby 需要的 C++ 代码呢?我尝试使用这些代码,但出现错误:
测试.cpp
static int add(int a, int b) // I tried without static too
{
return a + b;
}
编译
g++ --shared test.cpp -o test.dll
主要.rb
Win32API.new('test.dll', 'add', 'II', 'I').call(1, 2) # => should return 3
错误信息
RunTimeError
GetProcAddress: add or addA
版本:
Ruby 1.8.1
GCC 4.5.2 (tdm-1)
最佳答案
您需要使用 .DEF 文件。请参阅我对此的回答 question
关于c++ - 在 Ruby 上调用 Dll(不是 Windows API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697059/