开发者

Cython: call function from external C file

开发者 https://www.devze.com 2023-02-01 03:27 出处:网络
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

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.

0

精彩评论

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