[LLVMdev] Binary output to cout on Windows

Chris Lattner sabre at nondot.org
Tue May 23 11:12:06 PDT 2006


On Tue, 23 May 2006, Michael Smith wrote:
> The solution (provided in Microsoft's documentation) is to add:
> #include <cstdio>
> #include <io.h>
> #include <fcntl.h>
> and run:
>  int result = _setmode( _fileno(stdin), _O_BINARY );
>  if( result == -1 )
>  { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1;
> }
>  result = _setmode( _fileno(stdout), _O_BINARY );
>  if( result == -1 )
>  { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1;
> }
> before using cin or cout. I'm not sure where to add this however, since
> it needs to be called before any reads or writes are done, but only
> needs to be called once. Any suggestions? At the moment in my own code,
> I'm adding them to the tools source files, since that's where it's
> determined that cin/cout can be used for input/output.

Reid is the guru here, but I'll inject my opinion.  I think that this 
should be a method somewhere in the System library.  Given that, you 
should change llvm-as.cpp (and all other tools with similar issues) from:

       } else {                      // Specified stdout
         // FIXME: cout is not binary!
         Out = &std::cout;
       }

To:

       } else {                      // Specified stdout
         sys::ChangeStandardStreamToBinary(std::cout);
         Out = &std::cout;
       }

... where "ChangeStandardStreamToBinary" is something that Reid likes.  :)

The implementation of ChangeStandardStreamToBinary would do the code above 
in the win32 case, and would be a noop in the unix case.  This is just one 
way to do it, I defer to Reid for what the "right" way to do it is :)

-Chris



> -----Original Message-----
> From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu]
> On Behalf Of Chris Lattner
> Sent: Monday, May 22, 2006 4:36 PM
> To: LLVM Developers Mailing List
> Subject: RE: [LLVMdev] Binary output to cout on Windows
>
> On Mon, 22 May 2006, Michael Smith wrote:
>> llvm-as interprets "-" as using standard output (cout), so llvm-as <
>> input.ll -o - | opt has the same behavior. You'll actually find a
>> comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope
> that
>> there's a good way to do it.
>
> Please see:
> http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13
>
> We'd welcome patches to do this, and we can localize this in our system
> support library, but you'd have to figure out the magic to make this
> happen on win32.
>
> -Chris
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/




More information about the llvm-dev mailing list