Run build for C++ code (make) in Python script
<p>I keep running into an error when I want to compile code written in C++ using Python script to run "make" in x directory. Compiling the code takes about few seconds so I am using time module to have the script sleep for 60 seconds to finish compiling the code.</p>

<p><strong>Here is the code:</strong></p>

<pre><code>from subprocess import call
from time import sleep
def make_ut_adsmain():
        os.system("make ../../ads/main/unittest")
        # call(["ls", "-l"])
        sleep(60)

make_ut_adsmain()

# Run other functions when compiled...
</code></pre>

<p><strong>Error</strong> </p>

<pre><code>make: *** No rule to make target `../../ads/main/unittest'.  Stop.
</code></pre>

<p>The code never really compiles because the rest of the code depends on the log file which never gets created. Please guide me.</p>

<p>I actually figured it out and able to run the build process by using the following code:</p>

<pre><code>import subprocess
from time import sleep
def make_ut_adsmain():
        subprocess.Popen(["make"], stdout=subprocess.PIPE, cwd="../../ads/main/unitest")
        sleep(60)

make_ut_adsmain()
</code></pre>

<p>Note, the previous solution works only if you need to bypass errors. The correct way to do is the following. Check @tdelaney comment and read more on python docs <a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">https://docs.python.org/2/library/subprocess.html</a></p>

<pre><code>import subprocess
from time import sleep
def make_ut_adsmain():
        subprocess.check_all(["make"], cwd="../../ads/main/unitest")

make_ut_adsmain()
</code></pre>

python c++ shell scripting