simplest possible Newbie question

1,502 views
Skip to first unread message

Tom Elliott

unread,
Dec 23, 2010, 2:12:32 PM12/23/10
to cython-users
After Cython's "Hello World" and the example of calling a function in
the C math library, my next step would be to have a single C function
of my own in a separate file and call it from Cython. Following this

http://docs.cython.org/src/userguide/source_files_and_compilation.html#cython-files-depending-on-c-files

I modify the setup.py file to add this:

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

hello2.c:

#import <stdio.h>

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

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

(main is just there to show me that I can compile and run this on its
own and it's fine).

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 any working example with a simple C file or explain
what I'm doing wrong?

It seems like what I wanted might be here
http://docs.cython.org/src/userguide/tutorial.åhtml#fibonacci-fun

but big chunks are missing.

Thanks.

Robert Bradshaw

unread,
Dec 23, 2010, 3:11:07 PM12/23/10
to cython...@googlegroups.com
On Thu, Dec 23, 2010 at 11:12 AM, Tom Elliott <tell...@hsc.wvu.edu> wrote:
> After Cython's "Hello World" and the example of calling a function in
> the C math library, my next step would be to have a single C function
> of my own in a separate file and call it from Cython. Following this
>
> http://docs.cython.org/src/userguide/source_files_and_compilation.html#cython-files-depending-on-c-files
>
> I modify the setup.py file to add this:
>
> sourcefiles = ['hello2_caller.pyx', 'hello2.c']

Don't list hello2.c in the file, as it will get linked in twice. (The
cdef extern from creates an #include for you.) You don't want a main
in there either. Also, for hello2.c, you need #include not #import.

Tom Elliott

unread,
Dec 23, 2010, 3:52:31 PM12/23/10
to cython-users
OK, I fixed my silly error with #include and changed sourcefiles, but
I still get the same error.

hello2.c:

#include <stdio.h>

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

hello2_caller.pyx:

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

cpdef myf():
f()

setup:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

sourcefiles = ['hello2_caller.pyx']
ext_modules = [Extension("hello2_caller",
sourcefiles
)]

setup(
name = 'Hello2 app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)

error:

Christopher Barker

unread,
Dec 23, 2010, 4:08:40 PM12/23/10
to cython...@googlegroups.com, Tom Elliott
I barely know C, but...

On 12/23/10 12:52 PM, Tom Elliott wrote:
> OK, I fixed my silly error with #include and changed sourcefiles, but
> I still get the same error.

It's a warning, not an error, does the resulting executable work?

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

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

In modern C, the compiler expects both a declaration and a prototype.
Traditionally, the prototype goes in the *.h file, and the function
declaration itself goes in the *.c file.

however, I'm not getting the warning on my system (OS-X 10.6, gcc 4.2, I
think).

but you might try adding a prototype to make the compiler happy:

hello2.c:

#include <stdio.h>

void f();

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


-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Tom Elliott

unread,
Dec 23, 2010, 4:14:58 PM12/23/10
to cython-users
wrt the second suggestion I tried that and get:

hello2.c:3: warning: function declaration isn’t a prototype
hello2.c:5: warning: function declaration isn’t a prototype

on the first, hello2_caller.so is present but:

>>> import hello2_caller
>>> hello2_caller.f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'f'

Tom Elliott

unread,
Dec 23, 2010, 4:31:00 PM12/23/10
to cython-users
my bad..

it works when you call the cpdef'd function!
so it's just a warning issue..

Lisandro Dalcin

unread,
Dec 23, 2010, 9:01:53 PM12/23/10
to cython...@googlegroups.com
On 23 December 2010 18:14, Tom Elliott <tell...@hsc.wvu.edu> wrote:
> wrt the second suggestion I tried that and get:
>
> hello2.c:3: warning: function declaration isn’t a prototype
> hello2.c:5: warning: function declaration isn’t a prototype
>

Could you try this:?

#include <stdio.h>

void f(void);


void f(void) {


printf("%s", "Hello world!\n");
}


f() and f(void) are not the same thing in C, f(void) is likely the one you want.


--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Tom Elliott

unread,
Dec 24, 2010, 8:26:40 AM12/24/10
to cython-users
Yes, that fixed it. I posted about it:
http://telliott99.blogspot.com/2010/12/cython-3-my-own-c-source-file.html

And plan to do more simple examples.
Thanks to all.

On Dec 23, 9:01 pm, Lisandro Dalcin <dalc...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages