A .cc file in Sage's source code

58 views
Skip to first unread message

Nathann Cohen

unread,
Sep 22, 2013, 4:34:53 AM9/22/13
to Sage devel
Hellooooooooo everybody !

Would anybody know if there is an easy way to have a .c, .cpp or .cc file in Sage's source code, and have it compiled "manually", i.e. with a call to gcc and not through Cython ? I have been having some problems with some C++ code which cannot be written in Cython only (#13352), and all my attempts at getting it compiled through Cython ended up in mysterious segfaults.

The only way I see to make this work right now is to write a spkg, which would be far too much. We just want to make the popcount CPU instruction used by Sage when it is available.

Have fuuuuuuun everybody !

Nathann

Felix Salfelder

unread,
Sep 22, 2013, 4:58:55 AM9/22/13
to sage-...@googlegroups.com
On Sun, Sep 22, 2013 at 10:34:53AM +0200, Nathann Cohen wrote:
> Would anybody know if there is an easy way to have a .c, .cpp or .cc file
> in Sage's source code, and have it compiled "manually", i.e. with a call to
> gcc and not through Cython

there are plenty of cases of this kind.
grep -e '\.c\>' -e '\.cc\>' -e '\.cpp\>' src/module_list.py

also, there is a proper build system waiting for integration, #14807.

> Have fuuuuuuun everybody !

yes.

regards
felix

Nathann Cohen

unread,
Sep 22, 2013, 6:44:34 AM9/22/13
to Sage devel
Yoooooooooooooo !!!

> there are plenty of cases of this kind.
> grep -e '\.c\>' -e '\.cc\>' -e '\.cpp\>' src/module_list.py

Hmmm... I really tried this way (that's what I called "compiling it
through Cython" in my message), and I really can't get it to work. The
only thing that does it for me now is a .pxd file with a "cdef extern
from 'myfile.cc'", and it produces a segfault >_<

> also, there is a proper build system waiting for integration, #14807.

Hmmmmm O_O

Nathann

Felix Salfelder

unread,
Sep 22, 2013, 7:08:53 AM9/22/13
to sage-...@googlegroups.com
Hi Nathann.

On Sun, Sep 22, 2013 at 12:44:34PM +0200, Nathann Cohen wrote:
> Hmmm... I really tried this way (that's what I called "compiling it
> through Cython" in my message).

i see. a closer look into module_list.py reveals that the c/cc code is
just added to cython autogenerated code. urghs. is this what you
tried/want to do?

> and I really can't get it to work. The
> only thing that does it for me now is a .pxd file with a "cdef extern
> from 'myfile.cc'", and it produces a segfault >_<

if yourfile.cc contains the implementation, you probably want to add a
header with just the prototypes, and cdef from there (but i'm not so
totally sure about what cython/distutils will do with it -- cross your
fingers properly).

have fun
felix

Nathann Cohen

unread,
Sep 22, 2013, 7:12:31 AM9/22/13
to Sage devel
Hellooooooooo !!

> i see. a closer look into module_list.py reveals that the c/cc code is
> just added to cython autogenerated code. urghs. is this what you
> tried/want to do?

At first yes, but it seems that Cython does something to the C
function before using them, and that this interferes with c++
multiversionning which has the same purpose : functions have several
definitions in the .cc file, and at runtime the most adapted to the
current architecture is the only one that will be used.

That's just guessing, as all I can get is a Segfault :-/

> if yourfile.cc contains the implementation, you probably want to add a
> header with just the prototypes, and cdef from there (but i'm not so
> totally sure about what cython/distutils will do with it -- cross your
> fingers properly).

Yep. But that's how I get my segfault, unfortunately :-/

I'll resume the fight :-P

Nathann

Felix Salfelder

unread,
Sep 22, 2013, 7:28:21 AM9/22/13
to sage-...@googlegroups.com
On Sun, Sep 22, 2013 at 01:12:31PM +0200, Nathann Cohen wrote:
> Yep. But that's how I get my segfault, unfortunately :-/

does it run outside of sage/distutils magic (with cython and gcc called
manually)? should be easier to debug...

> I'll resume the fight :-P

good luck
felix

Nathann Cohen

unread,
Sep 22, 2013, 8:10:27 AM9/22/13
to Sage devel
Helloooooooooo !!

> does it run outside of sage/distutils magic (with cython and gcc called
> manually)? should be easier to debug...

Hmmm... I just made it work by compiling the .cc file as a shared
library, and linking the .pyx with the library in modules_list.py.

But I can't get it to work by having Cython compile this cursed .cc file :-P

Now it is telling me that I define several methods twice. Ahahahah.
This thing is a nightmare ;-)

So is there any place in Sage where you can write a couple of calls to
gcc, to make a library out of a .cc file ? The only place I know is a
spkg, and that's too much trouble ^^;

> good luck

Thaaaaaaaaaaaaaaaaaaaaaanks ! :-P

Nathann

Volker Braun

unread,
Sep 22, 2013, 8:22:12 AM9/22/13
to sage-...@googlegroups.com
Multiversioning is, at least for now, a C++ feature. You can link C and C++ code together, but you need to put extern "C" {} in the right places. But then the extern "C" function can't be multiversioning, so you need another layer of indirection for C code. In any case, post what you have...

Nathann Cohen

unread,
Sep 22, 2013, 8:27:19 AM9/22/13
to Sage devel
Hellooooooooooo !!

> Multiversioning is, at least for now, a C++ feature. You can link C and C++
> code together, but you need to put extern "C" {} in the right places. But
> then the extern "C" function can't be multiversioning, so you need another
> layer of indirection for C code. In any case, post what you have...

Hmmmmm... But didn't Cython support C++ now ? Or was it just classes ?
It is compiled with g++ anyway !

Here is my current code (it touches bitsets a bit and recompiles a lot
of things, sorry 'bout that), which is funny in its own way : I am
being accused of defining some things twice, and that's because Cython
links the final files with two copies of the (apprently identical) .so
files >_<

Nathann
a.patch

Felix Salfelder

unread,
Sep 22, 2013, 8:30:36 AM9/22/13
to sage-...@googlegroups.com
On Sun, Sep 22, 2013 at 02:10:27PM +0200, Nathann Cohen wrote:
> > does it run outside of sage/distutils magic (with cython and gcc called
> > manually)? should be easier to debug...
>
> Hmmm... I just made it work by compiling the .cc file as a shared
> library, and linking the .pyx with the library in modules_list.py.

what you probably want to make work is one pyx and one cc in one python
object. a library and linking sounds like overkill.

> But I can't get it to work by having Cython compile this cursed .cc file :-P

by having /distutils/ compile it? does it work if you compile manually?
something like

$ cython somefile.pyx -o somefile.cc
$ c++ somefile.cc -shared -fPIC <whateverelse> -o somefile.o
$ c++ yourfile.cc -shared -fPIC <whateverelse> -o yourfile.o
$ c++ yourfile.o somefile.o -o somemodule.so

> Now it is telling me that I define several methods twice. Ahahahah.
> This thing is a nightmare ;-)

sounds much like forgotten #ifndef something\n#define someting and
#endif in your header... (?)

> So is there any place in Sage where you can write a couple of calls to
> gcc, to make a library out of a .cc file ? The only place I know is a
> spkg, and that's too much trouble ^^;

without distutils, that would be easy :/

regards
felix
Reply all
Reply to author
Forward
0 new messages