我熟悉Ruby/DL,但不确定如何使用具有返回参数指针的C函数调用 [英] I am familiar with Ruby /DL but not sure how to use the C function calls that have pointers for return parameters
问题描述
我在此模块中具有此功能
I have this function in this Module
require 'dl'
require 'dl/import'
module LibCalendars
extend DL::Importer
dlload './cal2jd.o'
extern 'int iauCal2jd(int, int, int, double *, double *)'
end
如何在模块中进行设置以访问指针?
How do I set this up in a Module to get access to the pointers?
那里是我的部分需要吗?我不确定.如何正确完成? 确切的代码位于 http://www.iausofa.org/2013_1202_C/sofa/cal2jd. c 我需要知道如何访问那些地址指针. 验证测试在这里 http://www.iausofa.org/2013_1202_C/sofa/t_sofa_c. c
Is what I need partly in there? I'm just not sure. How is this done correctly? The exact code is at http://www.iausofa.org/2013_1202_C/sofa/cal2jd.c I need to know how to access those address pointers. The validation tests are here http://www.iausofa.org/2013_1202_C/sofa/t_sofa_c.c
几乎等效的main C看起来类似于下面的代码. 我只是复制了cal2jd.c文件,并在其底部添加了main函数.
An almost equivalent main C would look something like this next code. I just made a copy of the cal2jd.c file and tacked the main function in at the bottom of it.
#include <stdio.h>
int
main()
{
int y = 2003, m = 6, d = 1;
int j;
double djm0, djm;
double *pdjm0 = &djm0;
double *pdjm = &djm;
printf("values are: y == %d, m == %d, d == %d\n", y, m, d);
j = iauCal2jd(y, m, d, &djm0, &djm);
printf("j == %d\n", j);
printf("address of &djm0 == %p, size of pointer == %d\n", &djm0, sizeof(*pdjm0));
printf("address of &djm == %p, size of pointer == %d\n", &djm, sizeof(*pdjm0));
printf("value from &djm0 == %.20g, size of djm0 == %d\n", *pdjm0, sizeof(djm0));
printf("value from &djm == %.20g, size of djm == %d\n", *pdjm, sizeof(djm));
printf("value from &djm0 == %.3f\n", (float)*pdjm0);
printf("value from &djm == %.3f\n", (float)*pdjm);
return 0;
}
我用> gcc -o cal2jd cal2jd.c编译它
I compile it with > gcc -o cal2jd cal2jd.c
这是我的输出
$ cal2jd
values are: y == 2003, m == 6, d == 1
j == 0
address of &djm0 == 0022FF30, size of pointer == 8
address of &djm == 0022FF28, size of pointer == 8
value from &djm0 == 2400000.5, size of djm0 == 8
value from &djm == 52791, size of djm == 8
value from &djm0 == 2400000.500
value from &djm == 52791.000
状态为好,并且通过头文件sofam.h中的常量将djm0设置为2400000.5 您可以从源文件中的注释中看到它应该总是那样. 您可以从验证测试文件中看到djm应该是什么.
The status is good and djm0 is set by a constant in the header file sofam.h to 2400000.5 and you can see from the notes in the source file that it should always be that. You can see from the validation test file what djm should be.
我正尝试一次做太多事情. 在使用SOFA代码并尝试将其包装在Ruby中的同时,我正在学习一些C代码. (实际上是JRuby)所以我将为C部分,DL或FFI或一些很好的参考链接提供答案.
I'm trying to do too much at once. I'm learning some C code while working with the SOFA code and trying to wrap that in Ruby. (JRuby actually) So I'll take answers for the C part, the DL or FFI or some good reference links.
我也在考虑采用ffi方法. 这是一个开始.
I'm also considering ffi approach. Here is a start.
require 'ffi'
module MyLibrary
extend FFI::Library
ffi_lib "cal2jd.so"
attach_function :iauCal2jd, [:int, :int, :int, :pointer, :pointer], :int
end
我真的在JRuby中尝试了这个. 因为迷路了,我该从哪里去指针呢? 我可以举一些好例子吗? 预先感谢.
I'm really trying this in JRuby. Where do I go from here with pointers because I'm lost. Can I get some good examples please? Thanks in advance.
推荐答案
我发现了一种可以完成我最近打算的方法.如果有人要尝试此功能,我会在这里与您分享.首先,您必须将C库编译为共享对象文件.询问C程序员该怎么做.这就是我使用cal2jd.so的方式.
I've discovered a way to do what I had intended just recently. Here I will share it with you in case anyone wants to try this. First you have to compile your C library into a shared object file. Ask the C programmers how to do that. This is how I used the cal2jd.so.
require 'ffi'
module Calendars
extend FFI::Library
ffi_lib 'cal2jd.so'
attach_function :iauCal2jd, [:int, :int, :int, :pointer, :pointer ], :int
end
mp1 = FFI::MemoryPointer.new(:double, 8)
mp2 = FFI::MemoryPointer.new(:double, 8)
cal = Calendars.iauCal2jd(2014, 4, 6, mp1, mp2 )
puts cal
puts mp1.get_double, mp2.get_double
我的输出是
0 # return result good
2400000.5
56753.0 # pretty soon we're gonna have a straight 56789 ;-)
希望这可能对某人有所帮助,并感谢大家的帮助.继续留下更多评论或更好的答案,我们可以投票.
Hope this might help somebody and thanks all for helping. Go ahead and leave more comments or better answers we can vote on.
这篇关于我熟悉Ruby/DL,但不确定如何使用具有返回参数指针的C函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!