简体   繁体   English

在 python 中执行 C 程序?

[英]Executing a C program in python?

I have this C program, at least I think it is (files: spa.c, spa.h).我有这个 C 程序,至少我认为它是(文件: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?)有什么方法可以从 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 4 Ok, I got it working.更新 4好的,我开始工作了。 Program outputs values using printf:程序使用 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!谢谢大家!

There is no such thing as a C script .没有C 脚本这样的东西。 If you meant a C program you need to compile spa.c and spa.h into an executable before running it.如果您指的是C 程序,则需要在运行之前将spa.cspa.h编译为可执行文件。

If you use GCC in 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 spa program from your Python script with:之后,您可以使用 Python 脚本运行spa程序:

from subprocess import call
call(["./spa", "args", "to", "spa"])

cinpy comes 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)

C is not a scripting language. C 不是脚本语言。 You have to compile spa.c into an executable.您必须将 spa.c 编译为可执行文件。 You don't say your OS, but if Mac or Linux, try你不说你的操作系统,但如果是 Mac 或 Linux,试试

  gcc spa.c -o spa

If that works, you now have a executable named spa.如果可行,您现在就有了一个名为 spa 的可执行文件。 You can use python's os.system() to call it.您可以使用 python 的os.system()来调用它。

This is source code. 这是源代码。 You have to compile it before doing anything with it. 你必须在做任何事情之前编译它。 It is not executable as is. 它不是可执行的。

没有编译 C 程序所需的 C脚本之类的东西……如果编译为可执行文件,则可以使用os.system(CommandLine)执行它

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-aC-program我参考了 Quora 上的帖子: https : //www.quora.com/How-do-I-write-a-Python-script-to-run-aC-program

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

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