编译C code的字符串 [英] Compile string of C code

查看:105
本文介绍了编译C code的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的在这里下车在墙上的问题,而是有没有办法来编译GCC C code的字符串没有任何介质认为字符串(例如,一个源文件)?

Really off the wall question here, but is there a way to compile a string of C code in GCC without any medium to hold that string (eg. a source file)?

线沿线的东西:

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

感觉很肮脏的,但如果有一些简单的方法来做到这类型的事情这将是非常好的。

Feels really dirty, but it would be really nice if there was some simple way to do that type of thing.

推荐答案

如果您使用 - 在命令行中输入文件,GCC从标准输入读取。由于没有文件扩展名GCC可以用来找出应当编译的语言,它具有与 -x 标志指定:

If you use - as the input file in the command line, gcc reads from standard input. Since there is no file name extension gcc could use to find out the language that should be compiled, it has to be specified with the -x flag:

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

这篇关于编译C code的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆