Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best compile C++/Cython project into an executable?

I have a project with a bunch of C++ and Python/Cython files. Until now I primary developed the C++ part and compiled it to a static library with qmake. Some few methods are exposed with boost::python, and executed from a .py file.

I now wanted to compile the whole thing into a standalone executable.

My question now: what is the best way to do this? I tried to switch to Cython, compile the python files and linking the library. But it seems there is no direct way with distutils/setup.py to compile an executable, only shared libraries.

Is there a way to easily compile both .cpp and .pyx files into an executable at once?

So that I can get rid of a lot of the boost::python wrapper stuff and get a neat mix of c++/python without having to import a shared library and pack the whole stuff with pyinstaller?

like image 512
snøreven Avatar asked May 27 '12 16:05

snøreven


People also ask

How do I compile Cython to exe?

To compile the Cython source code to a C file that can then be compiled to an executable you use a command like cython myfile. pyx --embed and then compile with whichever C compiler you are using.

Can Cython be as fast as C?

Cython code runs fastest when “pure C” If you have a function in C labeled with the cdef keyword, with all of its variables and inline function calls to other things that are pure C, it will run as fast as C can go.


1 Answers

You should look into:

  • pyinstaller (or py2exe) for windows/linux
  • py2app for osx

Since python is your entry point, you will be able to bundle a stand-alone interpreter, environment, and resource location into an app/exe/binary. It will collect all your library modules into its self-contained site-packages

If you don't use any normal pure py files and only have cython files, then it is also possible to embed an interpreter into one of them as an entry point with an --embed flag to cython: http://wiki.cython.org/EmbeddingCython
Note, this is a similar "freeze" approach to the previously mentioned packaging options, but doesn't go the extra length to build a self contained env

like image 56
jdi Avatar answered Sep 19 '22 20:09

jdi