开发者

Cython linking to custom C code

I'm trying to use sage to run a ba开发者_开发问答sic Cython program that uses a custom C library.

I have three files: hello.h, hello.c, and cpy.spyx.

hello.h:

#include <stdio.h>  
void chello();

hello.c:

#include "hello.h"  
void chello() {  
  printf("Hello world\n");  
}

cpy.spyx:

#cinclude /home/sage/sage  
cdef extern from "/home/sage/sage/hello.h":  
  void chello()  
def pyhello():  
  chello()

I'm trying to run this in sage using (only) the command:

load "cpy.spyx"

I get the following error:

Import Error /home/sage/sage//temp/... : undefined symbol: chello This is my first attempt at Cython, so I may have a stupid mistake in my code. An alternate theory is that the .h file is not being copied to the temp directory above.

Thanks


Finally found the solution here: http://trac.sagemath.org/sage_trac/ticket/1994

There are some apparently otherwise-undocumented directives.

Using the same .c and .h file as above, I used the following .spyx file:

#clang c  
#cfile hello.c  
#cinclude /home/sage/sage  
cdef extern from "/home/sage/sage/hello.h":  
  void chello()  
def pyhello():
  chello()

Note the differences between the link and my code above: I didn't include spaces after the #, and I didn't put quotes around the path in the cinclude line. This is a good example of a hello world program for Cython using Sage.

I put all three files (.c, .h, and .spyx) in the /home/sage/sage directory. Then I ran sage and started the program with

load cpy.spyx

No other steps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜