简体   繁体   English

使用python子进程编译c代码时出现错误代码1

[英]Getting error code 1 when compiling c code using python subprocess

I want to compile c code using GCC via Python subprocess.我想通过 Python 子进程使用 GCC 编译 c 代码。 I have installed MinGW gcc for this.我已经为此安装了 MinGW gcc。 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.当我在命令行上运行命令时,生成了可执行文件,但是当我尝试通过 Python 子进程执行相同操作时,我得到 1 个退出代码,因此没有生成可执行文件。 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谢谢

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确保您从 Python 正确调用 GCC
  2. Capture any errors or warnings from your Windows command shell从 Windows 命令外壳捕获任何错误或警告
  3. Capture any errors or warnings from GCC从 GCC 捕获任何错误或警告

Here are some examples from the Python documentation:以下是 Python 文档中的一些示例:

https://docs.python.org/3/library/subprocess.html 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'')

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

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