This is not a question about C/C++ as such, but I couldn't think of where else to put it.
I would like to know how to call either gcc or g++ and specifying that all error messages that are to occur when compiling some source code are to be written to a file. for example..

// HelloWorld.cpp
#include <iostream>
using namespace std;

int main()
{
  cout<<"Hello World\n";
  this_is_my_code_error;
}

I have tried to call g++ like this to get the error..

g++ "C:\HelloWorld.cpp" > "C:\output.txt"

the file output.txt was created but it did not contain anything, i.e. the error the compiler found in the source code was not written to that file.

I'm running Windows XP.

Thanks.

Try this:

g++ "C:\HellowWorld.cpp" 2> "C:\output.log"

You could probably also do:

g++ "C:\HellowWorld.cpp" 1>2> "C:\output.log"

Just in case stdout did produce any output.

2 specifies that "standard error" or "stderr" or errors be redirected; 1 is standard output or "stdout" - that is, anything that the program returns as non-errors.

g++ probably outputs error messages to stderr instead of stdout. try this:

g++ "C:\HelloWorld.cpp" 2> "C:\output.txt"

[edit]Oops! too late. ^^^ is right[/edit]

Thanks, it works perfectly.
Is this "2>" a g++ functionality or an OS functionality? I.e. will the same command work on Unix i.e.

g++ "~/HelloWorld.cpp" 2> "~/out.txt"

Cheers.

. will the same command work on Unix .

yes :)

Hi,

i'm trying the below command on bash

ls "king" 2> error.log
ls: cannot access king: No such file or directory
ls: cannot access 2: No such file or directory


I still am not able to redirect the error to error.log
#king doesn't exist obviously...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.