Compile a line of C code

Indeed, the question about the wall is here, but is there a way to compile a line of C code in GCC without any environment to store that line (for example, the source file)?

Something along the lines of:

$ gcc "#include <stdio.h> int main( void ){ printf('hello world'); return 0;}" -o test

The feeling is really dirty, but it would be very nice if there was an easy way to do it.

+4
source share
3 answers

If you use -command line as input, gcc reads from standard input. Since the file name extension is not used to find out the language that should be compiled, it must be specified using the flag -x:

$ gcc -x c -o tst - <<EOF
> #include <stdio.h>
> int main(void) {
>   printf("Hello world\n");
> }
> EOF
$ ./tst
Hello world
+7
source

, :

(cat preamble.hpp; xsel) | g++ -x c++ - && ./a.out

"-x c" C.

+5
source

All Articles