1

I want to compile c code using GCC via Python subprocess. I have installed MinGW gcc for this. When I run the command on the command line, the executable is made, but when I try to do the same via Python subprocess, I get 1 exit code, and hence no executable is made. The command in question :

C:/MinGW/bin/gcc E:/some/folders/main.c

What I'm trying to do

import subprocess

print(subprocess.run('C:/MinGW/bin/gcc E:/some/folders/main.c', shell=True))

Is there something fundamentally wrong here with how I am approaching this ? Thanks

2
  • 1
    Check stdout & stderr from the process object. Commented Oct 29, 2021 at 19:18
  • PS: I don't happen to have Python 3 handy, or I would have given you a Windows example. But please check out my response below, and the links I cited. Please remember that there could be any of SEVERAL different "root causes". Your best bet it to get a "meaningful error message", and troubleshoot from there. Commented Oct 29, 2021 at 21:29

1 Answer 1

0

I get 1 exit code, and hence no executable is made.

You SHOULDN'T rely exclusively on the "exit code". Instead, you need to:

  1. Ensure you're invoking GCC correctly from Python
  2. Capture any errors or warnings from your Windows command shell
  3. Capture any errors or warnings from GCC

Here are some examples from the Python documentation:

https://docs.python.org/3/library/subprocess.html

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.