3

Is it possible to pipe the contents of a file into g++ to compile a program?

I want to do this because I want to use a file from a database rather than a physical file on a disk. The file contents can be easily retrieved via an API I have made.

For instance, I would like to do something like:

g++ contents_of_file -o executable

Thanks a lot.

Sam.

2 Answers2

4

Yes, you can pipe to gcc if you specify the language using the -x option;

echo "int main(){}" | gcc -Wall -o testbinary -xc++ -
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 1
    seems like choice of gcc/g++ is important; i get link errors when trying to use `gcc -xc++` on c++ code, but linking succeeds with `g++ -xc++` – assem Aug 04 '14 at 00:45
0

more fun:

alias true="gcc -xc++<(echo 'main()<%int(*)(int*)=6 "\?"<[:0:>;%>')&&a.out"

hey, works for me.

aminorex
  • 126
  • 5