简体   繁体   English

分割错误-Python-> C

[英]segmentation fault - Python -> C

My code is: 我的代码是:

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

I converted it to C code using cython: 我使用cython将其转换为C代码:

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

Then compiled C file using gcc: 然后使用gcc编译C文件:

~ $ 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. 我上面提到的python代码只是一个示例。 I tried doing the same with other python programs I have written. 我尝试对我编写的其他python程序执行相同的操作。 For all of them, I'm getting the same error. 对于所有这些人,我都会遇到相同的错误。
How to solve this? 如何解决呢?

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. 尝试执行共享库(这是通过在GCC中使用-shared标志来构建的库)将导致分段错误。 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 Compilation的说明,该说明清楚地表明您所使用的命令是用于编译扩展模块(可以从Python import C代码)。 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. Cython 不是用于制作独立的Python程序,而是用于将Python扩展模块编译为C。您仍然需要运行Python解释器才能使用它们。

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? 如果您想将Python代码编译为独立的二进制文件(无论是什么意思-除静态链接的二进制文件外,所有其他二进制文件都对系统库有依赖性),您可能想看一下这样的问题: 如何制作Python脚本独立可执行文件,无需任何依赖即可运行?

You can use Nuitka , which is a Python compiler than can produce standalone executables that I've heard good things about. 您可以使用Nuitka (这是一个Python编译器),它可以生成我听说过的好东西的独立可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM