编译C代码的字符串

Jos*_*lin 6 c gcc compilation

真的不在墙上的问题,但有没有办法在GCC中编译一串C代码,没有任何媒介来保存该字符串(例如,源文件)?

有点像:

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

感觉真的很脏,但如果有一些简单的方法来做这种事情,那将是非常好的.

sth*_*sth 8

如果-在命令行中用作输入文件,gcc将从标准输入读取.由于没有文件扩展名,gcc可以用来查找应编译的语言,因此必须使用-x标志指定:

$ gcc -x c -o tst - <<EOF
> #include <stdio.h>
> int main(void) {
>   printf("Hello world\n");
> }
> EOF
$ ./tst
Hello world
Run Code Online (Sandbox Code Playgroud)


小智 6

我承认有时会突出显示代码并使用:

(cat preamble.hpp; xsel) | g++ -x c++ - && ./a.out
Run Code Online (Sandbox Code Playgroud)

对于C,"-x c"也是如此.