32

GCC is normally instructed to output to a file via the -o switch. If this isn't provided it seems to decide on an appropriate name and output to that. How do I make GCC write its generated output to stdout?

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526

3 Answers3

38
gcc -o /dev/stdout foo.c

Note that /dev/stdout is defined as a symlink: /dev/stdout -> /proc/self/fd/1.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Now, is there a similar solution for Windows? Or will I have to hack it for cross-platform solutions? – Kevin Cox Jul 27 '13 at 19:26
  • @Kevin, wild guess, try `-o CON`? It's been a while since I've done Windows. :/ – sarnold Jul 29 '13 at 21:14
  • 5
    **A word of caution on Cygwin**: `gcc -o /dev/stdout foo.c` outputs nothing, but `gcc -c -o /dev/stdout foo.c` **deletes `/dev/stdout`!** – Matt Feb 19 '15 at 15:59
  • 1
    @Matt So how to make GCC output to stdout on Cygwin? – pmor Aug 26 '21 at 09:35
27

You can use -o-, for example to print an assembly listing:

gcc -S -o- in.c
ergosys
  • 47,835
  • 5
  • 49
  • 70
  • 9
    It's important to note that this only works for some outputs. For example `gcc -o- test.c` creates an executable called `-`. – Kevin Cox Jul 27 '13 at 19:24
  • Its good to understand `-o-` as actually `-o -`. `-` refers to standard input/output depending on the context. – Hari Mar 17 '23 at 11:55
-1

Um, what are you going to do with a binary object file dumped to stdout? anyway, some programs accept the '-' (single minus, no quotes) character as replacement for stdout. If you're on linux, you can do -o /dev/fd/1

zvrba
  • 24,186
  • 3
  • 55
  • 65