在 python 中执行 C 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4376397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing a C program in python?
提问by Izz ad-Din Ruhulessin
I have this C program, at least I think it is (files: spa.c, spa.h). Is there any way I can execute this script from Python WITHOUT passing extra arguments to the Python interpreter (if not, what would the arguments be?)
我有这个 C 程序,至少我认为它是(文件:spa.c、spa.h)。有什么方法可以从 Python 执行这个脚本而不将额外的参数传递给 Python 解释器(如果没有,参数是什么?)
Update: Thanks for your replies. The source code can be found at http://www.nrel.gov/midc/spa/#register
更新:感谢您的回复。源代码可以在http://www.nrel.gov/midc/spa/#register找到
(Please do not be scared by the 'register' in the url, if you fill in the form, you can immediately download the files (no validation mails, etc) I will try your suggestions and report back with the results.
(请不要被 url 中的“注册”吓到,如果您填写表格,您可以立即下载文件(没有验证邮件等)我会尝试您的建议并报告结果。
Update 2: I compiled the source code using gcc, but now it gives me a permission denied when trying to call(), even when running python as root (im on Ubuntu 10:10).
更新 2:我使用 gcc 编译了源代码,但现在它在尝试调用()时给了我一个权限被拒绝,即使在以 root 身份运行 python 时也是如此(我在 Ubuntu 10:10 上)。
Update 3[Errno 8] Exec format error
更新 3[Errno 8] Exec 格式错误
Update 4Ok, I got it working. Program outputs values using printf:
更新 4好的,我开始工作了。程序使用 printf 输出值:
>>> call(['/path'])
Julian Day: 2452930.312847
L: 2.401826e+01 degrees
B: -1.011219e-04 degrees
R: 0.996542 AU
H: 11.105902 degrees
Delta Psi: -3.998404e-03 degrees
Delta Epsilon: 1.666568e-03 degrees
Epsilon: 23.440465 degrees
Zenith: 50.111622 degrees
Azimuth: 194.340241 degrees
Incidence: 25.187000 degrees
Sunrise: 06:12:43 Local Time
Sunset: 17:20:19 Local Time
Thanks all!
谢谢大家!
采纳答案by Pablo Santa Cruz
There is no such thing as a C script. If you meant a C programyou need to compile spa.cand spa.hinto an executable before running it.
没有C 脚本这样的东西。如果您指的是C 程序,则需要在运行之前编译spa.c并spa.h生成可执行文件。
If you use GCCin Linux or Mac OS X:
如果您在 Linux 或 Mac OS X 中使用GCC:
$ gcc -Wall spa.c -o spa
Will get you an executable named spa.
将为您提供一个名为spa.
After that, you can run spaprogram from your Python script with:
之后,您可以使用以下命令spa从 Python 脚本运行程序:
from subprocess import call
call(["./spa", "args", "to", "spa"])
回答by kriss
This is source code. You have to compile it before doing anything with it. It is not executable as is.
这是源代码。在对它做任何事情之前,你必须先编译它。它不能按原样执行。
回答by Lou Franco
C is not a scripting language. You have to compile spa.c into an executable. You don't say your OS, but if Mac or Linux, try
C 不是脚本语言。您必须将 spa.c 编译为可执行文件。你不说你的操作系统,但如果是 Mac 或 Linux,试试
gcc spa.c -o spa
If that works, you now have a executable named spa. You can use python's os.system()to call it.
如果可行,您现在就有了一个名为 spa 的可执行文件。您可以使用 pythonos.system()来调用它。
回答by tobyodavies
there is no such thing as a C scriptyou need to compile C programs... if you compile to an executable, you can execute it using os.system(CommandLine)
没有编译 C 程序所需的 C脚本之类的东西……如果编译为可执行文件,则可以使用os.system(CommandLine)
回答by John La Rooy
cinpycomes close using the awesome combination of tcc and ctypes
cinpy非常接近使用 tcc 和 ctypes 的组合
The following code is ripped from cinpy_test.py included in the package.
以下代码是从包中包含的 cinpy_test.py 中提取的。
import ctypes
import cinpy
# Fibonacci in Python
def fibpy(x):
if x<=1: return 1
return fibpy(x-1)+fibpy(x-2)
# Fibonacci in C
fibc=cinpy.defc("fib",
ctypes.CFUNCTYPE(ctypes.c_long,ctypes.c_int),
"""
long fib(int x) {
if (x<=1) return 1;
return fib(x-1)+fib(x-2);
}
""")
# ...and then just use them...
# (there _is_ a difference in the performance)
print fibpy(30)
print fibc(30)
回答by shantanu pathak
Question is old still I felt one important part is missing!
问题老了还是觉得少了一个重要的部分!
C code can be compiled AND executed within Python script:
C 代码可以在 Python 脚本中编译和执行:
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World\n";
return 1;
}
import subprocess
subprocess.call(["g++", "hello_world.cpp"]) # OR gcc for c program
tmp=subprocess.call("./a.out")
print("printing result")
print(tmp)
$ python main.py
Hello World
printing result
1
I referred to the post on Quora: https://www.quora.com/How-do-I-write-a-Python-script-to-run-a-C-program
我参考了 Quora 上的帖子:https: //www.quora.com/How-do-I-write-a-Python-script-to-run-aC-program

