0

I wrote a little python script, intending to automate non-default options for gcc (on Kubuntu 14.04); the python runs without error now, and inserting a debug print statement (or changing the system command to 'echo') verifies the correct information is being passed, but I get an error from gcc saying

$ python gccm prog16
gcc: fatal error: no input files 
compilation terminated.

Here's the script I wrote:

#!/usr/bin/python
from sys import argv #get incoming argument
import subprocess #function to call an OS program

script, target = argv

# massage received argument into form needed for math.h linkage
target = "-o " + target + " " + target + ".c -lm"
subprocess.call (['gcc', target], shell=False)`

There are other additions I'd make to the gcc call (compile version options, stricter code checking, etc.), if I can get this to work correctly. Based on the error message, it appears to be invoking gcc correctly, but the target source file isn't being found; could this not be running in the directory from which I invoke it? If so, how can I get it to run from the correct directory (where I'm keeping my C source code files); if not, what else could cause this?

2 Answers 2

1

If you're using shell=False, your arguments to the sub-processes shouldn't be concatenated together. Instead, they should each be their own element in the args list:

subprocess.call(['gcc', '-o', target, target+'.c', '-lm'], shell=False)

On a related note, any reason why you're writing something like this yourself? If you're looking to use a Python-based build system, have a look at SCons.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, that would have been a useful thing to mention in the documentation for subprocess (at least the version I found and read). :p I'm writing this myself because a) I can use the practice with python (it's only half a dozen lines), and b) I'm not really looking for a python-based build system, or any build system, to add another layer of things I have to learn while I'm trying to learn python itself, and relearn C, as well as c) I had no idea there was such a tool and didn't know to or how to look for it.
No problem - just thought I would point out a related application. Enjoy your project!
1

If you have shell=False, then you must pass each argument separately into the subprocess.call.

Try this instead:

subprocess.call (['gcc', '-o', target, target + '.c', '-lm'], shell=False)

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.