简体   繁体   English

如何将scipy.weave.inline与外部C库一起使用?

[英]How do I use scipy.weave.inline together with external C libraries?

I am trying to understand weave.inline to wrap C code in my Python programs. 我试图理解weave.inline在我的Python程序中包装C代码。 The code below simply takes the Numpy array and multiplicates all of its elements by 2. 下面的代码简单地使用Numpy数组并将其所有元素乘以2。

inl.py inl.py

import numpy
import scipy.weave

a = numpy.array([1.0, 2.0, 3.0])
N = a.shape[0]

print a
code = \
  """
  int i;
  for(i = 0; i < N; i++)
  {
    a[i] = a[i] * 2;
  }
  """

scipy.weave.inline(code, ['a','N'])
print a

Then I want to carry some functions from inline code to external libraries. 然后我想将内联代码中的一些函数传递给外部库。 Let it be the trivial multiplication by 2. So I create two files: 让它成为2的琐碎乘法。所以我创建了两个文件:

mult.c mult.c

#include "mult.h"

float mult(float n)
{
  return n * 2;
}

mult.h mult.h

float inc(float n);

Now I want to use function mult in my inline code. 现在我想在我的内联代码中使用函数mult。 But I don't know how do I link my C files with Python inline code. 但我不知道如何将我的C文件与Python内联代码链接起来。 I tried to compile C files as shared library and pass them as headers and libraries in weave, but that was in vain. 我试图将C文件编译为共享库,并将它们作为头文件和库传递给编织,但这是徒劳的。 Any suggestions? 有什么建议么?

I have successfully done this, calling math functions from R via weave.inline() code (under Ubuntu Linux). 我已成功完成此操作,通过weave.inline()代码(在Ubuntu Linux下)从R调用数学函数。

First, compile your C functions as a shared library. 首先,将C函数编译为共享库。 In my case, I grabbed a recent release of R from CRAN, and did 就我而言,我从CRAN手中抢到了最近发布的R版本

./configure --enable-R-static-lib --enable-static --with-readline=no
cd src/nmath/standalone/
make

You should now have a file called libRmath.so . 您现在应该有一个名为libRmath.so的文件。 If libpath is a string with the directory that holds libRmath.so , you can do something like 如果libpath是一个包含libRmath.so的目录的字符串,那么你可以这样做

code = 'return_val = pbinom(100, 20000, 100./20000., 0, 1);'
support_code = 'extern "C" double pbinom(double x, double n, double p, int lower_tail, int log_p);'
weave.inline(code, support_code=support_code,
    library_dirs=[libpath], libraries=["Rmath"], runtime_library_dirs=[libpath])

Note a couple things. 注意几件事。 The header declarations have to go in support_code , not code (I don't know why), and they have to be prefixed with extern "C" because they're C code, not C++ (this is standard). 头部声明必须在support_code ,而不是code (我不知道为什么),并且它们必须以extern "C"为前缀,因为它们是C代码,而不是C ++(这是标准的)。 It should be possible to include headers files instead of using support_code (check the docs for weave.inline), but I haven't tried it. 应该可以包含头文件而不是使用support_code (检查weave.inline的文档),但我还没有尝试过。 The library name is Rmath , but the shared library file is libRmath.so , in the usual Unix convention. 库名是Rmath ,但共享库文件是libRmath.so ,在通常的Unix惯例中。 And the path to the library is specified twice, once for linking, and once for execution. 并且指定库的路径两次,一次用于链接,一次用于执行。

Hope this helps! 希望这可以帮助!

put the source of mult.c and mult.h in a string object called extra_code, then add the following line in your .weave invocation 将mult.c和mult.h的源代码放在名为extra_code的字符串对象中,然后在.weave调用中添加以下行

support_code=extra_code,

there is also the option to include standard libraries as follows: 还可以选择包含标准库,如下所示:

headers = ["<math.h>"]

enjoy 请享用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM