gpt4 book ai didi

c++ - 在 Ruby 上调用 Dll(不是 Windows API)

转载 作者:行者123 更新时间:2023-11-28 03:50:28 28 4
gpt4 key购买 nike

我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com