0

If i have a rather large library of files, and want to link them, do i create a bash script and then put that into a python script to get all the files i need for that terminal line i need to use to compile. I don't think im building it properly cause there is no ./test

#!/usr/bin/env python3
import os
import subprocess
file = open("react3dEngine.txt", "r")

fileList=file.readlines()
file.close()

p=subprocess.Popen(["/usr/bin/g++", "-Wall", str(fileList), "-lglut", 
"-lGLU", "-lGL", "-stdio=c+11", "-o", "test", 'main.cpp'], 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
p=subprocess.Popen(["./test"], stdout=subprocess.PIPE,  
stderr=subprocess.PIPE)
p.communicate()
1
  • -stdio=c+11 didn't you mean -std=c++11? You should include any errors you get, also try cutting and pasting the command you build into the command line directly to see if that works. Commented Oct 29, 2017 at 9:25

2 Answers 2

3

The problem is likely with str(fileList); you are telling g++ to look for a file named "['foo.c', 'bar.c']" (the stringification of the list). You should have something like the following instead

["/usr/bin/g++", "-Wall"] + fileList + ["-lglut", "-lGLU", "-lGL", "-std=c+11", "-o", "test", 'main.cpp']
Sign up to request clarification or add additional context in comments.

1 Comment

-std=c++11 and took off the capture of errors, now i can see that my batch script isn't getting the file path i would have thought readlink -f foo.cpp would have worked. Thanks.
1

fileList is a list. Calling str() on a list produces a formatted string that includes the brackets, e.g.

>>> fileList = ['a.c', 'b.c', 'c.c']
>>> str(fileList)
"['a.c', 'b.c', 'c.c']"

so that's not going to work as part of a command line.

Instead you need to pass each of the strings in the file list as an argument. You also need to remove the trailing new lines that might be present in the file that you read with readlines() (I don't know the format of your react3dEngine.txt file).

Try coding it like this:

with open("react3dEngine.txt") as files:
    file_list = [line.strip() for line in files]
    p = subprocess.Popen(["/usr/bin/g++", "-Wall"] + file_list + ["-lglut", 
"-lGLU", "-lGL", "-std=c+11", "-o", "test", 'main.cpp'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # etc.

The strip() takes care of any leading and trailing whitespace that might be present in the input file.

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.