C++ 在Ruby上调用DLL(不是Windows API)
我所处的Ruby环境无法加载C扩展,只能加载纯Ruby代码。我不能使用RubyLine、FFI或Win32OLE,只有一种加载外部代码的方法:Win32API。但如何编译Ruby所需要的C++代码呢?我尝试使用这些代码,但出现错误: test.cppC++ 在Ruby上调用DLL(不是Windows API),c++,ruby,winapi,C++,Ruby,Winapi,我所处的Ruby环境无法加载C扩展,只能加载纯Ruby代码。我不能使用RubyLine、FFI或Win32OLE,只有一种加载外部代码的方法:Win32API。但如何编译Ruby所需要的C++代码呢?我尝试使用这些代码,但出现错误: test.cpp static int add(int a, int b) // I tried without static too { return a + b; } 编撰 g++ --shared test.cpp -o test.dll main.r
static int add(int a, int b) // I tried without static too
{
return a + b;
}
编撰
g++ --shared test.cpp -o test.dll
main.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文件。看到我的答案了吗