Missing extern C prototypes for some public functions in a C++ project

243 views
Skip to first unread message

Bastien Léonard

unread,
May 15, 2012, 2:02:45 AM5/15/12
to cython-users
I have a C++ Cython project which has a few public
functions. These functions are called from a C++ file that
contains code that I couldn't write in Cython. The project
doesn't run anymore, because most of these functions can't be
found at runtime. The reason is that the .h file generated by
Cython contains __PYX_EXTERN_C declarations, but most the
functions aren't declare with that macro in the generated C++
file (for some reason, one public function does get declared with
extern C). So the shared object exports the mangled function
name. If I add the missing prototypes, the project runs
correctly.

It's difficult to provide a minimal example. I tried to create a
file with just a bunch of public functions, and none of them got
defined with and extern C prototype.

Here is the project's code, in case you're interested:
https://github.com/bastienleonard/pysfml2-cython
(I believe convert_to_vector2f(), wrap_time_instance() and
wrap_render_states_instance() are the only public functions which
get a prototype.)

Joseph Galbraith

unread,
Jun 8, 2012, 5:22:16 PM6/8/12
to cython-users
I believe I'm having the same problem. Did you figure out how to fix
this?

The following small example shows the problem:

x.pyx:
cdef extern from *:
ctypedef struct wchar_t
ctypedef wchar_t* LPCWSTR "const wchar_t*"

cdef public int xPy_foo(LPCWSTR szScriptName):
return 0

xx.cpp:
#include "Python.h"
#include "x.h"

int
main(int argc, char** argv)
{
xPy_foo(L"");
}

build:
$ cython --cplus x.pyx
$ g++ -I/usr/include/python2.7 -std=c++11 xx.cpp x.cpp -lpython2.7
/tmp/cc7xBcE5.o: In function `main':
xx.cpp:(.text+0x15): undefined reference to `xPy_foo'
collect2: error: ld returned 1 exit status

If I add extern "C" to the the xPy_foo definition in x.cpp, it will
link.

I'm using cython built from a git clone that I just made.

(I know the code is bogus, but it does demonstrate the problem.)

Thanks,

Joseph

On May 15, 12:02 am, Bastien Léonard <bastien.leon...@gmail.com>
wrote:
> I have a C++ Cython project which has a few public
> functions. These functions are called from a C++ file that
> contains code that I couldn't write in Cython.  The project
> doesn't run anymore, because most of these functions can't be
> found at runtime. The reason is that the .h file generated by
> Cython contains __PYX_EXTERN_C declarations, but most the
> functions aren't declare with that macro in the generated C++
> file (for some reason, one public function does get declared withexternC). So the shared object exports the mangled function

Bastien Léonard

unread,
Jun 15, 2012, 1:31:19 PM6/15/12
to cython-users
The difference is that I get the error at runtime. That may be
because I'm using a setup.py script and you are using the command
line directly. If I understand correctly, your command line will
try to create an a.out file instead of a shared object.

The only solution I've found is to create source releases with
the fixed C++ file so that people can build the project
easier. If they really want to build from Git, I have guidelines
about how to add the missing prototypes in the generated file.

Cheers,
Bastien Léonard

Bradley Froehle

unread,
Jun 15, 2012, 1:44:05 PM6/15/12
to cython...@googlegroups.com
Sounds like you need to set language='c++' in setup.py.  See http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#specify-c-language-in-setup-py

Bastien Léonard

unread,
Jul 28, 2012, 7:05:18 PM7/28/12
to cython...@googlegroups.com
As far as I can tell, this seems to be a bug in Cython. Should I
file a bug report, or should I assume that a Cython developer
would do it if it's appropriate?

> Sounds like you need to set language='c++' in setup.py.  See

It's already set as c++.


Cheers,
Bastien

jcopeland

unread,
Aug 1, 2012, 8:20:54 PM8/1/12
to cython...@googlegroups.com
Any word on this issue?  I'm bumping into the same situation as Joseph running cython from commandline with --cplus option. 


The following small example shows the problem:

x.pyx:
cdef extern from *:
    ctypedef struct wchar_t
    ctypedef wchar_t* LPCWSTR "const wchar_t*"

cdef public int xPy_foo(LPCWSTR szScriptName):
    return 0

xx.cpp:
#include "Python.h"
#include "x.h"

int
main(int argc, char** argv)
{
    xPy_foo(L"");
}

build:
$ cython --cplus x.pyx
$ g++ -I/usr/include/python2.7 -std=c++11 xx.cpp x.cpp -lpython2.7
/tmp/cc7xBcE5.o: In function `main':
xx.cpp:(.text+0x15): undefined reference to `xPy_foo'
collect2: error: ld returned 1 exit status

If not an error, can someone explain the "proper" way to do what is being done in Joseph's example above?

Lisandro Dalcin

unread,
Aug 2, 2012, 11:33:16 AM8/2/12
to cython...@googlegroups.com
I've recently pushed a fix for this issue to cython-dev...
https://github.com/cython/cython/commit/01adf0000e88b70eaf3a7315cfeaf80aa4e21bc1

Could you give it a try?
--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

jcopeland

unread,
Aug 2, 2012, 1:20:22 PM8/2/12
to cython...@googlegroups.com


On Thursday, August 2, 2012 8:33:16 AM UTC-7, Lisandro Dalcin wrote:
I've recently pushed a fix for this issue to cython-dev...
https://github.com/cython/cython/commit/01adf0000e88b70eaf3a7315cfeaf80aa4e21bc1

Could you give it a try?


Fix worked in my scenario and in the example provided.  Thanks!
Reply all
Reply to author
Forward
0 new messages