0

I need to run a C file from my python program. I also want to pass arguments from my program. I want the output back in my python program.

simple example I tried on the c code:

#include<stdio.h>
int main(){
int a=2;
return a;
}

In my jupyter notebook I tried :

import subprocess as sb
sb.call(["g++","random.c"],shell=True)  ##random.c is the C file.
sb.call("./a.out",shell=True)

I am getting the output status as 1( I guess some error). How to get the return value of the C code ?

gag123
  • 357
  • 1
  • 2
  • 8
  • Check out `ctypes` - and your C function probably does *not* want to be called `main` – alani Sep 12 '20 at 20:07
  • 1
    Even `subprocess` would work if all you want to do is run the program and pass in some data. – Carcigenicate Sep 12 '20 at 20:09
  • Is this C program an executable or is it in a library and you want to call it directly from python? – tdelaney Sep 12 '20 at 20:16
  • @Carcigenicate Can you please provide the code for it.I am not being able to do it using subprocessOptionals like capture_output is not working...The C program is in the same directory. – gag123 Sep 13 '20 at 10:08

1 Answers1

1

I wrote a demo on the subprocessing module a while back that addresses this.

Here is the example I used:

import os # Used to determine if machine is windows or unix/macOS
import subprocess # Used to run commands from python

def compile(filename, binary_filename):
    """Function to compile the provided file in gcc"""
    # Below is equivalent to running: gcc -o hello_world hello_world.c
    print(f"Creating binary: {binary_filename} From source file: {filename}\n")
    subprocess.run(["gcc", "-o", binary_filename, filename])

def run_binary(binary_filename):
    """Runs the provided binary"""
    print(f"Running binary: {binary_filename}\n")
    subprocess.run([binary_filename])

if __name__ == "__main__":
    compile("hello_world.c", "hello_world")

    if os.name =="nt": # If on windows
        run_binary("hello_world.exe")

    else: # If on unix/mac
        run_binary("hello_world")

    print("Binary run")

I think this answers your question, if instead you want to call C code from python you will need the ctypes library.

If you are looking to go the other way, and run python from C code you can follow the answer to this question.

Kieran Wood
  • 1,297
  • 8
  • 15
  • 1
    This is an interesting solution but it makes a lot of assumptions about the environment - like you have `gcc` and no extra include files, libraries or other parameters needed on the command line. And there needs to be a way to get the result of the function back to python. But its also like the `make;make install` workflow used on a lot of projects. – tdelaney Sep 12 '20 at 20:21
  • @tdelaney yeah the demo is mostly to explain what subprocessing does. The C code example is just to demonstrate it's usefulness in using python to script a build process. You technically could attach to ```stdin``` before running the ```subprocess``` call to get a result, or pipe the result into a temp file. I'm just not %100 sure what the OP wants. – Kieran Wood Sep 12 '20 at 20:26
  • The printf statement in my hello_world program is not getting printed. And also how to capture the return value . – gag123 Sep 13 '20 at 09:53