8

This is my try:

CMD file:

@SET PATH=%PATH%;D:\mingw\bin
type test10.cpp | g++ -xc++ -o test10.exe

code (irrelevant here): int main() {}

error I get:

g++: fatal error: no input files
compilation terminated.

I thought that the -x option is for signalizing stdin input, gcc itself said me that.

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • 2
    possible duplicate of [Is it possible to get gcc to read from a pipe?](http://stackoverflow.com/questions/1003644/is-it-possible-to-get-gcc-to-read-from-a-pipe) – ks1322 Mar 03 '13 at 09:45

2 Answers2

16

The -x option specifies the input language, but it doesn't tell g++ to read from stdin. To do that, you can pass a single dash as a file name.

type test10.cpp | g++ -o test10.exe -x c++ -
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

you can use here doc in the interactive shell. for example:

gcc -x c - <<eof
#include <stdio.h>

void foo()
{
    int a = 10;
    static int sa = 10;

    a += 5;
    sa += 5;

    printf("a = %d, sa = %d\n", a, sa);
}


int main()
{
    int i;

    for (i = 0; i < 10; ++i)
        foo();
}
eof

reference

jianyongli
  • 1,131
  • 12
  • 10