title: Node.js, g++ (gcc) and child process - gcc cannot open output file error
body: <p>For programming competition, I am making simple HTML/Javascript page that takes your sources, compiles and evaluates them. This is my code for running GCC:</p>
  
  <pre><code>request.onfiles = function(files) {
    // Add output file name at the end of params
    files.push("-o output/main.exe");
    // Evaluate correct working path, that's /upload/username          
    var cwd = path.resolve(process.cwd(), './upload/'+request.name+"/");
    console.log("&gt;&gt;  "+cwd+"/g++ ", files.join(" "));
    console.log(process.cwd());
    // Run process
    var gcc = request.process = childProcess.spawn('g++',files,{env: process.env, cwd: cwd});
    gcc.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
    });
    // Error output
    gcc.stderr.on('data', function (data) {
      console.log('stderr: ' + data);
    });
    gcc.on('close', function (code) {
      console.log('child process exited with code ' + code);        
      _this.working.splice(_this.working.indexOf(request));
      _this.emit("action.compile", request.name);
      _this.tryContinue();
    });
  }
  </code></pre>
  
  <p>Node.js process runs at project root and GCC is launched in <code>/upload/[USERNAME]/</code>. The <code>/upload/username</code> directory contains sourcecode:</p>
  
  <p><img src="https://i.stack.imgur.com/7QQxz.png" alt="image description"></p>
  
  <p>My debug output looks like this:</p>
  
  <pre><code>Compile by  username  requested.
  &gt;&gt;  D:\web\soutez\upload\username/g++  main.cpp -o output\main.exe
  D:\web\soutez
  stderr: c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin
  stderr: /ld.exe: cannot open output file  output\main.exe: No such file or directory
  stderr:
  collect2.exe: error: ld returned 1 exit status
  
  child process exited with code 1
  </code></pre>
  
  <p>If I actually copy the line with the command (<code>g++  main.cpp -o output\main.exe</code>) and go in the <code>upload/username</code>, compilation works:</p>
  
  <p><img src="https://i.stack.imgur.com/YkLV9.png" alt="image description"></p>
  
  <p>I also noticed that although GCC <strong>can allways find the source</strong>. If I add error in <code>main.cpp</code>, node.js output looks like this:</p>
  
  <pre><code>&gt;&gt;  D:\web\soutez\upload\username/g++  main.cpp -o output\main.exe
  D:\web\soutez
  stderr: main.cpp:2:2: error: 'XXX' does not name a type
    XXX
    ^
  
  child process exited with code 1
  </code></pre>
  
  <p>I need some help, I don't understand what is node.js doing different. Worst thing: if I remove <code>/output</code>, it works:</p>
  
  <pre><code>files.push("-o main.exe");
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/LzH9c.png" alt="image description"></p>