.C() returns 我是一个空列表
.C() returns me an empty list
我是 R 的初学者,我正在尝试将用 C 编写的名为 dll.dll
的 .dll
文件加载到 R 中。它似乎可以工作,现在我想要使用存储在 .dll
文件中的函数时遇到问题。
我在此处和 google 上的手册中搜索了解决方案或其他方法。如果我能得到有关使用什么或任何想法的建议,将不胜感激!
我的代码:
setwd("C:/Users/MyUser/R")
dyn.load("dll.dll")
is.loaded("DLL_FUNK")
# For some reason True with capital letters, not in lower case
output <- .C("DLL_FUNK", in9 = as.integer(7))
#output # R Crashes before I can write this.
# R Crashes
# In outdata.txt: "in-value= 139375128"
该函数应该 return 一个数字,1955。但我似乎无法获得该值。我究竟做错了什么?
更新代码(Fortran 运行ned as C),这是 dll.dll 中的代码:
subroutine dll_funk(in9)
implicit none
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!*** Declarations: variables, functions
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer(4) :: in9
!integer :: in9
! Definitions of variables in the external function calls
!!dec$ attributes c,alias :'dll_funk' :: dll_funk
!dec$ attributes dllexport :: dll_funk
!dec$ attributes value :: in9
open(194,file='outdata.txt')
write(194,*) 'in-value=', in9
! in9 = 1955
close(194)
end subroutine
!end function
所以现在当它 运行s 时,R 崩溃了,但在它写入我的文件之前 (outdata.txt) 但它不是我的号码,可能是某种地址...
另一个问题,你是推荐我 运行 带有 .C 的代码和来自 C 运行 Fortran 代码,还是 运行 带有 .Fortran 的代码更好Fortran 代码?
.Fortran 似乎在处理字符串时遇到问题,或者这就是我从以下内容中了解到的:Interface func .C and .Fortran
你为什么不给你的 C 函数传递任何参数 dll_function
?当您使用 .C()
时,您必须将函数参数作为列表传递。 .C()
将return 修改列表。所以,如果你什么都不传入,你什么也得不到。
你的 C 函数 dll_function
是什么样子的?注意:
dll_function
必须是 void
C 函数,没有 return 值。如果这个函数应该return一些东西,它必须return通过修改函数参数;
dll_function
的所有函数参数必须是指针。
跟进
The dll_function is only to test if I can get access to it.
可以在dyn.load()
之后使用is.loaded()
来测试是否可以访问C函数:
dyn.load("dll.dll")
is.loaded("dll_function") ## TRUE
请注意,is.loaded
采用 C 函数名称,而 dyn.load()
采用 .dll
名称。通常,您可以在一个 .dll
文件中包含多个函数。您可以使用is.loaded()
检查其中任何一个,以测试是否已成功加载共享库。
So if I want it to return something, I should give it an argument (of same type?)?
是的。这里的另一个答案确实给出了一个玩具示例。你可以看看我半个月前做的。底部有变量类型的总结。
当使用 .C
时,传递给 .C
的额外参数被复制并作为指向被调用的 c 函数的指针传递。然后这个函数可以修改指针指向的数据指针。函数的 return 值被 .C
忽略。所以,您的 c 函数应该类似于:
void dll_function(int* result) {
/* Do some complicated computation that results in 1955 */
(*result) = 1955;
}
你从 R 打来的电话:
.C("dll_function", integer(1))
带有输入的示例(计算整数向量的总和;此示例假定向量中没有缺失值):
void dll_function2(int* result, int* vector, int* length) {
int sum = 0;
for (int i = 0; i < (*length); ++i, ++vector) {
sum += (*vector)
}
(*result) = sum;
}
从 R 调用:
x <- c(1000, 900, 55)
.C("dll_function2", integer(1), as.integer(x), length(x))[[1]]
我是 R 的初学者,我正在尝试将用 C 编写的名为 dll.dll
的 .dll
文件加载到 R 中。它似乎可以工作,现在我想要使用存储在 .dll
文件中的函数时遇到问题。
我在此处和 google 上的手册中搜索了解决方案或其他方法。如果我能得到有关使用什么或任何想法的建议,将不胜感激!
我的代码:
setwd("C:/Users/MyUser/R")
dyn.load("dll.dll")
is.loaded("DLL_FUNK")
# For some reason True with capital letters, not in lower case
output <- .C("DLL_FUNK", in9 = as.integer(7))
#output # R Crashes before I can write this.
# R Crashes
# In outdata.txt: "in-value= 139375128"
该函数应该 return 一个数字,1955。但我似乎无法获得该值。我究竟做错了什么?
更新代码(Fortran 运行ned as C),这是 dll.dll 中的代码:
subroutine dll_funk(in9)
implicit none
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!*** Declarations: variables, functions
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
integer(4) :: in9
!integer :: in9
! Definitions of variables in the external function calls
!!dec$ attributes c,alias :'dll_funk' :: dll_funk
!dec$ attributes dllexport :: dll_funk
!dec$ attributes value :: in9
open(194,file='outdata.txt')
write(194,*) 'in-value=', in9
! in9 = 1955
close(194)
end subroutine
!end function
所以现在当它 运行s 时,R 崩溃了,但在它写入我的文件之前 (outdata.txt) 但它不是我的号码,可能是某种地址...
另一个问题,你是推荐我 运行 带有 .C 的代码和来自 C 运行 Fortran 代码,还是 运行 带有 .Fortran 的代码更好Fortran 代码? .Fortran 似乎在处理字符串时遇到问题,或者这就是我从以下内容中了解到的:Interface func .C and .Fortran
你为什么不给你的 C 函数传递任何参数 dll_function
?当您使用 .C()
时,您必须将函数参数作为列表传递。 .C()
将return 修改列表。所以,如果你什么都不传入,你什么也得不到。
你的 C 函数 dll_function
是什么样子的?注意:
dll_function
必须是void
C 函数,没有 return 值。如果这个函数应该return一些东西,它必须return通过修改函数参数;dll_function
的所有函数参数必须是指针。
跟进
The dll_function is only to test if I can get access to it.
可以在dyn.load()
之后使用is.loaded()
来测试是否可以访问C函数:
dyn.load("dll.dll")
is.loaded("dll_function") ## TRUE
请注意,is.loaded
采用 C 函数名称,而 dyn.load()
采用 .dll
名称。通常,您可以在一个 .dll
文件中包含多个函数。您可以使用is.loaded()
检查其中任何一个,以测试是否已成功加载共享库。
So if I want it to return something, I should give it an argument (of same type?)?
是的。这里的另一个答案确实给出了一个玩具示例。你可以看看我半个月前做的
当使用 .C
时,传递给 .C
的额外参数被复制并作为指向被调用的 c 函数的指针传递。然后这个函数可以修改指针指向的数据指针。函数的 return 值被 .C
忽略。所以,您的 c 函数应该类似于:
void dll_function(int* result) {
/* Do some complicated computation that results in 1955 */
(*result) = 1955;
}
你从 R 打来的电话:
.C("dll_function", integer(1))
带有输入的示例(计算整数向量的总和;此示例假定向量中没有缺失值):
void dll_function2(int* result, int* vector, int* length) {
int sum = 0;
for (int i = 0; i < (*length); ++i, ++vector) {
sum += (*vector)
}
(*result) = sum;
}
从 R 调用:
x <- c(1000, 900, 55)
.C("dll_function2", integer(1), as.integer(x), length(x))[[1]]