Skip to content
Advertisement

G++ cannot resolve paths when called from subprocess

I am writing a small python script to for automatic student homework evaluation. I have a skeleton, where submitted homework is unzipped into a temporary folder and my script is called with path to the temporary folder as an argument.

My script first copies over some extra files I need and then tries to compile c++ code roughly like this:

compilation_files = " ".join(glob.iglob("*.cpp"))
compilation_call = ["g++", "-std=c++14", compilation_files, "-o " + output_name]
subprocess.call(compilation_call)

g++ exits with this error:

g++: error: tests-main.cpp tests-small1.cpp small1.cpp: No such file or directory
g++: fatal error: no input files

I am sure the files exist (after all, glob wouldn’t be able to find them otherwise), and that the subprocess is executed in the correct directory.

I did this

print "ls: ", subprocess.check_output("ls")
print "pwd: ", subprocess.check_output("pwd")
print "files: ", compilation_files

to verify that the files are indeed there, are called properly and the script is being run in the correct directory.

— edit —

I can make the script work by passing shell=True, but that would also mean manually escaping file names for shell.

Advertisement

Answer

Hmm, you are calling a subprocess with a list of arguments. First is the name of the command that will be executed, following ones are the arguments of the command one at a time.

So you are executing something like:

g++ -std=c++14 "tests-main.cpp tests-small1.cpp small1.cpp" -o xxx

and the error says that the file (singular file not plural files) "tests-main.cpp tests-small1.cpp small1.cpp" does not exist.

You should instead do:

compilation_call = ["g++", "-std=c++14"] + glob.iglob("*.cpp") + [ "-o " + output_name]

to have each individual file name in its own parameter list item.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement