Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在Ruby上调用DLL(不是Windows API)_C++_Ruby_Winapi - Fatal编程技术网

C++ 在Ruby上调用DLL(不是Windows API)

C++ 在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

我所处的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.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文件。看到我的答案了吗