Cython: call function from external C file

After Cython's "Hello World" and the example of calling a function in the C math libraries here, what I really want to do is to have C code of my own in a separate file and use it from Cython. Following this, I modify the setup.py file:

sourcefiles = ['hello2_caller.pyx', 'hello2.c']

This is hello2.c (main is just there to compile and test it separately---though that product isn't present for the test:

#import <stdio.h>

void f() {
    printf("%s", "Hello world!n");
}

int main(int argc, const char* argv[]) {
    f();
    return 0;
}

This is hello2_caller.pyx

cdef extern from "hello2.c":
    void f()

cpdef myf():
    f()

I get:

In file included from hello2_caller.c:219:
hello2.c:3: warning: function declaration isn’t a prototype

So I guess I'm failing to provide a header in some way.. though just feeding setup.py a standard header like 'hello2.h' doesn't work. Can you point me to a working example or explain what I'm doing wrong. Thanks.


Thanks to help from the Cython users' list here.
My writeup here.

Bottom line: this is only a warning, that is not fixed by a declaration of f(), but the compiled .so works. I'm still not sure how you would provide a .h file to Cython or if there is a better way to do this.

And there's a couple of errors: should be #include, and don't list the .c file in sourcfiles.

链接地址: http://www.djcxy.com/p/49554.html

上一篇: 使用boost :: program

下一篇: Cython:从外部C文件调用函数