【问题标题】:Executing a C program in python?在python中执行C程序?
【发布时间】:2010-12-07 12:08:23
【问题描述】:

我有这个 C 程序,至少我认为是(文件:spa.c、spa.h)。有什么方法可以在不向 Python 解释器传递额外参数的情况下从 Python 执行这个脚本(如果没有,参数是什么?)

更新:感谢您的回复。源码在http://www.nrel.gov/midc/spa/#register

(请不要被网址中的“注册”吓到,如果您填写表格,您可以立即下载文件(没有验证邮件等)我会尝试您的建议并报告结果。

更新 2:我使用 gcc 编译了源代码,但现在它在尝试调用()时给了我一个权限被拒绝,即使以 root 身份运行 python(我在 Ubuntu 10:10 上) .

更新 3 [Errno 8] 执行格式错误

更新 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

谢谢大家!

【问题讨论】:

  • 您能否在此处发布您的spa.c 的一些摘录(或者,更好的是,整个代码)?尽管确实存在一些类似 C 的脚本实现,但通常 C 是一种编译语言。即使你找到一些用 Python 编写的 C 解释器,让任意 C 程序在其中工作的机会也很小。
  • 据我所知,没有任何参数可以将 Python 解释器转换为 C 编译器。但是您可以使用 python ctypes 从 dll 或共享库中运行 C 代码。

标签: python c


【解决方案1】:

没有C 脚本这样的东西。如果您的意思是C 程序,您需要在运行之前将spa.cspa.h 编译成可执行文件。

如果您在 Linux 或 Mac OS X 中使用 GCC

$ gcc -Wall spa.c -o spa

将为您提供一个名为 spa 的可执行文件。

之后,您可以在 Python 脚本中运行 spa 程序:

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

【讨论】:

  • args: 带参数的字符串应该是什么?例如:args = "--option for my program"?
  • 然后call(["your program", "--option", "for", "my", "program"]) 如果您确实希望该选项是多个参数。也许你的意思是call(["your program", "--option", "for my program"]),但只有你自己知道。
【解决方案2】:

cinpy 使用 tcc 和 ctypes 的出色组合非常接近

以下代码是从包中包含的 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)

【讨论】:

【解决方案3】:

C 不是脚本语言。您必须将 spa.c 编译成可执行文件。你没有说你的操作系统,但如果是 Mac 或 Linux,请尝试

  gcc spa.c -o spa

如果可行,您现在就有了一个名为 spa 的可执行文件。你可以使用python的os.system()来调用它。

【讨论】:

    【解决方案4】:

    没有像 C 脚本这样的东西,你需要编译 C 程序...如果你编译成可执行文件,你可以使用 os.system(CommandLine) 执行它

    【讨论】:

    • 你应该更喜欢subprocess而不是os.system()
    【解决方案5】:

    问题已经老了,我还是觉得缺少一个重要的部分!

    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
    

    我参考了 Quora 上的帖子:https://www.quora.com/How-do-I-write-a-Python-script-to-run-a-C-program

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2015-08-28
      • 1970-01-01
      • 2010-09-09
      • 2019-02-16
      • 1970-01-01
      • 2012-05-14
      • 2010-09-09
      相关资源
      最近更新 更多