Re: reading from stdin

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am Freitag, 2. September 2005 12:10 schrieb Erik Leunissen:
> L.S.
>
> I would like to compile C code by feeding it to gcc (or cc) from stdin,
> instead of specifying an input file. What I want would result in a
> command line like:
>
>      echo "ThisWouldNeedToBeCcode" | gcc -o out.o
>
>
> (Note aside:
> This may look as an awkward or even useless construct at first glance.
> However that's not the point. I supplied this example invocation to keep
> the case simple and to the point. In reality I want to invoke such a
> command from a scripting language, where not using files for input does
> make sense. I do not want to clutter this case with issues regarding the
> scripting language.)
>
>
> The above command line and several variants that I tried, evoke the
> following complaint:
>
>      gcc: no input files
>
> which, of course, is very much true.
>
>
> Is reading from stdin simply not supported?
>
>
> In case it is relevant, here's the output from "gcc -v":
>
> Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.1/specs
> Configured with: ../configure --enable-threads=posix --prefix=/usr
> --with-local-prefix=/usr/local --infodir=/usr/share/info
> --mandir=/usr/share/man --libdir=/usr/lib
> --enable-languages=c,c++,f77,objc,java,ada --disable-checking
> --enable-libgcj --with-gxx-include-dir=/usr/include/g++
> --with-slibdir=/lib --with-system-zlib --enable-shared
> --enable-__cxa_atexit i586-suse-linux
> Thread model: posix
> gcc version 3.3.1 (SuSE Linux)
>
>
>
> Thanks in advance for any help,
>
> Erik Leunissen
> ==============

If you try to feed gcc with stdin by specifing the input file as '-' (as it is 
wide spread standart)  gcc tells you :

	echo "some input"|gcc -

gcc: -E required when input is from standard input

which means (-E) only to execute the preprocessor state.  This reflects that 
only the preprocessor works as a simple stdin/stdout filter and that gcc, in 
common is a wrapper around several programs that will write temporary files 
anyway (if not suppressed by "-pipe").

This also makes sense, since a compile process is NOT progressive but final, 
which means you need a closed file to be compiled into an object file.

I would recommend to write an own wrapper producing the result you want:

gcc_stdin_wrap.sh:
#!/bin/sh
cat - > /tmp/tmp_file_name.c
gcc -c -o $* /tmp/tmp_file_name.c
rm /tmp/tmp_file_name.c

Of course you can add much more features (security !) to this script.


BYE INGO

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux