[Pyrex] newbie problem with wrapping c++ code

Sven Berkvens-Matthijsse sven at pyrex.berkvens.net
Thu Apr 10 09:47:08 CEST 2008


> Hello,

Hello,

> I'm a first-day newbie at pyrex, and I'm having some trouble
> wrapping some cpp code. I've spent quite a while searching around
> the internet, but I can't seem to discover what I'm doing wrong.
> 
> Here's my .pyx file:
> 
> ++++++++++++++++++++++++++++
> cdef extern from "ccode/test.h":
>     void hello_world()
> 
> def helloWorld():
>    hello_world()
> ++++++++++++++++++++++++++++
> 
> ccode/test.h is just:
> 
> ++++++++++++++++++++++++++++
> void hello_world();
> ++++++++++++++++++++++++++++
> 
> 
> and ccode/test.cpp is:
> 
> ++++++++++++++++++++++++++++
> #include <iostream>
> #include "test.h"
> 
> using namespace std;
> 
> void hello_world()
> {
>  cout << "hello, world." << endl;
> }
> ++++++++++++++++++++++++++++

Your hello_world() function is a C++ function, and that cannot be
called from C programs. You need to define it like this:

extern "C" void hello_world(void)
{
  cout << "hello, world." << endl;
}

> Anyway, the error I'm getting when I try to import the new module is:
> 
> >>> import test
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> ImportError: ./test.so: undefined symbol: hello_world

Correct, because the C++ symbol is called something like
"_11hello_world4void" (probably not this exactly, but something like
it) instead of "hello_world".

> What am I doing wrong?  I'm sure it's something very simple...
> 
> Thanks!
> --Hoyt

-- 
With kind regards,
Sven Berkvens-Matthijsse



More information about the Pyrex mailing list