title: Calling gcc from python script gives me 'Undefined symbols: "_main"
body: <p>I am trying to write a code generator script in Python, that generates a C-source file, compiles it and runs it. However, I am having trouble calling gcc from my script.</p>
  
  <p>A simple hello world example:</p>
  
  <pre><code>import subprocess  
  
  basename = "CodeGenTest";  
  execname = basename;  
  srcname = basename + ".c";  
  
  codeList = [];  
  codeList.append("#include &lt;stdio.h&gt;");  
  codeList.append("int main(int argc, char *argv[])\n{");  
  codeList.append("printf(\"Hello world.\\n\");");  
  codeList.append("}");  
  
  # Convert codelist to string.  
  codeList.append("");  
  codeString = "\n".join(codeList);  
  
  # Print code to output source file  
  outfile=open(srcname,'w');  
  outfile.write(codeString);  
  outfile.close;  
  
  print "Compile.";  
  cmd = ["gcc", "-O2", srcname, "-o", execname];  
  p = subprocess.Popen(cmd);  
  p.wait();  
  
  subprocess.call(["./"+execname]);  
  </code></pre>
  
  <p>If I run this script, I get the following error output</p>
  
  <pre><code>Compile.
  Undefined symbols:
    "_main", referenced from:
        start in crt1.10.6.o
  ld: symbol(s) not found
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>If I do the exact same thing in the python interpreter shell, it works fine. I can also compile the code without problems directly in the shell.</p>
  
  <p>I have tried various variants, using subprocess.Popen(), subprocess.call(), with and without all possible combinations of arguments that I can think of, still the same problem.</p>
  
  <p>Anyone has any idea what might be my problem here?</p>