10

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?

snøreven
  • 1,904
  • 2
  • 19
  • 39
  • Is the main entry point of your app via the python scripts and the C++ part is library? – jdi May 27 '12 at 17:27
  • Yes, the entry point is in Python. I write all expensive operations in C++ (which are quite a lot) and add additional functionality to the C++ classes in Python (inheritance). The main "flow", gui, networking stuff and so on is written in Python. – snøreven May 27 '12 at 17:50
  • The answer to [this question](http://stackoverflow.com/q/2581784/87699) gives pointers to compiling Cython directly to an executable. Your case is slightly different but it might still be useful. – Scott Griffiths May 28 '12 at 09:45
  • @scott-griffiths: Thanks. I already aware of that, but it didn't help me very much. I know there is even an '-embed' flag for cython, but I cannot get it working with distfiles, and I couldn't see how I could compile a big project with it. It just adds a main() function to the single file you compile. – snøreven Jun 02 '12 at 12:15

1 Answers1

1

You should look into:

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

Community
  • 1
  • 1
jdi
  • 90,542
  • 19
  • 167
  • 203
  • I think my question was a little bit ambiguous: I would prefer a solution where the python code is (or can also be) compiled. – snøreven May 27 '12 at 20:05
  • Packaging it this way doesnt prevent you from compiling your cython and c++ extensions and bundling them. You still need a python interpreter to launch the py code. – jdi May 27 '12 at 22:27
  • @snøreven: Yes maybe I am missing something about your questions. Are you trying to build your cpp+pyx into a single module? Because you said the word "executable" from your cpp+pyx, which is confusing to me. Regardless, you will need an interpreter for your entry point py file, and the only way to build python scripts into stand-alone executables is via this packaging approach. – jdi May 27 '12 at 22:37
  • You are right. I thought there was a way to directly compile everything (cython/python) at once. As I see it I have to compile the cpp/python stuff as modules and then calling them from a .py file which I can package? Is that correct? – snøreven Jun 02 '12 at 12:08
  • Yes that sounds correct. The py file would be the entry point. Its also meant to support a portable site-packages for your other py packages and a Resource location. See my update for another route with just cython as an entry – jdi Jun 02 '12 at 15:27