# How to Redirect Clang++ Diagnostics to a File?



## rtwingfield (Oct 2, 2013)

Seems that this should be an easy thing to do, redirect SDTOUT and STDERR of the clang++ compiler to a file; however, the diagnostic info continues to output to the display terminal if only STDOUT is implicitly specified, e.g., `# clang++ -v 'file' >x.out`, and an empty x.out file is created.  If STDERR is included in the command, `# clang++ -v 'file' >x.out 2>&1`, the system complains of "ambiguous redirect". My understanding of this construct is to redirect STDERR (2) to STDOUT (1) and both should be written to x.out.

Is there some other file pointer that clang is using for diagnostic output?


----------



## kpa (Oct 2, 2013)

Are you using tcsh(1) as your shell? The kinds of redirections you're trying to do are next to impossible in tcsh(1), use a Bourne compatible shell and the `clang++ -v 'file' >x.out 2>&1` redirection should work as expected. Then there is of course script(1) that captures the output of a command the same way regardless of the shell used.


----------



## SirDice (Oct 2, 2013)

rtwingfield said:
			
		

> If STDERR is included in the command, `# clang++ -v 'file' >x.out 2>&1`, the system complains of "ambiguous redirect".


You're trying Bourne shell style redirects on a C-shell. That's not going to work 

Redirection is a bit of a pain with csh(1). This should work though:
`# clang++ -v 'file' >& x.out`
(This redirects both STDERR _and_ STDOUT to x.out)

What csh(1) however cannot do is redirect STDOUT to one file and STDERR to another.


----------



## rtwingfield (Oct 2, 2013)

Yeah, that was it, running in tcsh. Switched to sh and it worked like a charm.


----------

