24

The code is quite simple :

test$ cat test.cpp

int main()
{
}

Is there a way to compile the code that is coming from a standard output?

I have tried this :

cat test.cpp | g++ -

and some variations, but none produced executable.


Just some clarifications. I have a program which preprocess a file, and produces another file which I want to compile. I thought about not creating this intermediate file, but to instead produce the object file directly.

Mark B
  • 93,381
  • 10
  • 105
  • 184
BЈовић
  • 59,719
  • 40
  • 167
  • 261
  • 2
    That’s stdin, not stdout. – Konrad Rudolph Jul 30 '13 at 13:09
  • 1
    Why are people so interested in compiling from standard input? Is it that hard to generate a file and compile that? – David Rodríguez - dribeas Jul 30 '13 at 13:13
  • 2
    @DavidRodríguez-dribeas My motto is : skip a middle man :) – BЈовић Jul 30 '13 at 13:14
  • 1
    Well, you are `cat`-ing a file... so the middle man is there. If your intention was typing directly into the compiler, then chances are that for anything not absolutely trivial you will make a typo and will have to retype (I know, I have used `cat` as an editor a few times which is fine if you never mistype)... – David Rodríguez - dribeas Jul 30 '13 at 13:20
  • Don't do that. If you generate C or C++ code, generate it in some temporary file (perhaps in a *tmpfs* filesystem like `/tmp/`), and fork a `gcc` or a `make` process to compile it. Read [my answer here](http://stackoverflow.com/a/17709877/841108) – Basile Starynkevitch Jul 30 '13 at 13:21
  • @DavidRodríguez-dribeas No, ´cat´ is not there. I have something else. A program which parses a header, and creates some code, which needs to be compiled into an obj file. – BЈовић Jul 30 '13 at 13:24
  • @BasileStarynkevitch Any specific reason not to do it? I do not really care for the speed of the compilation. – BЈовић Jul 30 '13 at 13:36
  • Read [my answer here](http://stackoverflow.com/a/17709877/841108) which gives a lot of reasons for not doing it. – Basile Starynkevitch Jul 30 '13 at 13:37

2 Answers2

40

The compiler would have probably told you:

-E or -x required when input is from standard input

Try

cat test.cpp | g++ -x c++ -
devnull
  • 111,086
  • 29
  • 224
  • 214
  • 3
    Yes, it told me, but it didn't tell to put c++ after. After adding c++ it works – BЈовић Jul 30 '13 at 13:16
  • 3
    @BЈовић In these cases, you are supposed to read the documentation of the particular compiler flag you will be using. `-x` is documented to require the language as its argument. –  Jul 30 '13 at 13:29
  • 3
    @H2CO3 The error message is not helpful at all (if you do not provide the language). Also, the ´-´ has to be at the end. Try to find such details in g++ documentation – BЈовић Jul 30 '13 at 13:34
  • @devnull Please explain the need for that last `-`; it appears it can be omitted, yet is somehow necessary? – GooseDeveloper Sep 09 '21 at 17:16
2

Did you try this ?

$ cat tst.cpp | g++ -x c++ -

I have just tried it under Cygwin and had no problem.

Shlublu
  • 10,693
  • 4
  • 51
  • 69