2

I'm trying to set up a cython setup that will compile a python source code to an executable (It should embed the main method within) - currently I have managed to set it up as an importable module but not as a standalone executable.

I saw that there is a compiler option Options.embed that should handle this. (In this post it said that it should be set to the function that the interpreter should call - main)

This is the module code:

def main():
    print('Cython Demo')

if __name__ == '__main__':
    main()

This is the setup "compile.py" code

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
from Cython.Compiler import Options

Options.docstrings = False
Options.emit_code_comments = False
Options.embed = "main"

ext_modules = cythonize([
    Extension("cython_demo.mymod.moduleA",["/home/myuser/PycharmProjects/cython_demo/mymod/moduleA.py"])],
    compiler_directives=dict(always_allow_keywords=True,language_level = '3'))

setup(
    name = 'My Program Name',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

Unfortunately after compiling the python code and trying to run the executable by calling:

./moduleA.cpython-36m-x86_64-linux-gnu.so

i get the segmentation error.

Segmentation fault (core dumped)

I saw that the main function is there by running grep "int main" on the file. What may be the problem?

When I'm importing the module from somewhere else and running main directly - it works:

import moduleA
moduleA.main()

Thanks!

sborpo
  • 928
  • 7
  • 15
  • 2
    A `.so` file ending usually means it is a shared library, not a standalone executable – UnholySheep Dec 23 '18 at 15:50
  • 1
    After searching around a bit it seems that it is not possible to compile a standalone executable by using `distutils`: see https://stackoverflow.com/questions/46824143/cython-embed-flag-in-setup-py and https://groups.google.com/forum/#!topic/cython-users/zcKA2E7Eo3c (both of which run a separate command to compile the generated C code afterwards) – UnholySheep Dec 23 '18 at 16:06

0 Answers0