0

My code is:

#!/usr/bin/python
import os
os.system('ls')

I converted it to C code using cython:

~ $ cython ostest.py
~ $ ls ostest*
ostest.c  ostest.py

Then compiled C file using gcc:

~ $ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \-I/usr/include/python2.7 -o ostest.so ostest.c
~ $ ls ostest*
ostest.c  ostest.py  ostest.so

And when I tried to execute the file, its giving error:

~ $ ./ostest.so
Segmentation fault

I checked the file permissions:

~ $ ls -l ostest.so
-rwxr-xr-x

The python code I mentioned above is just a sample. I tried doing the same with other python programs I have written. For all of them, I'm getting the same error.
How to solve this?

4
  • 1
    Why are you building a shared library? Commented Sep 4, 2015 at 13:59
  • 2
    Can you elaborate a bit which tutorial you are following? This looks very weird. Why are you building a shared library and then try to execute it via shell? That does not make much of a sense. Commented Sep 4, 2015 at 14:00
  • 1
    I'm trying to create a stand-alone executable file for linux from python code. I'm very new to Linux and Python. Commented Sep 4, 2015 at 14:05
  • You don't need to compile python. Commented Sep 4, 2015 at 14:20

1 Answer 1

3

Trying to execute a shared library (which is what you're building by using the -shared flag with GCC) is going to result in a segmentation fault. That's because you're not meant to run a shared library. It appears you've misread the instructions for Cython Compilation -- which clearly states that the command you've used is for compiling extension modules (C code which you can import from Python). Cython is not for making standalone Python programs, it's for compiling Python extension modules to C. You'll still need to run a Python interpreter to use them.

If you want to compile your Python code to be a standalone binary (whatever that means -- all binaries except statically linked binaries have some dependancy on system libraries), you might want to take a look at this SO question: How to make a Python script standalone executable to run without ANY dependency?

You can use Nuitka, which is a Python compiler than can produce standalone executables that I've heard good things about.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.