您可以将代码直接传递到gcc吗?例如:gcc-?'int main(){return 0;}' [英] Can you pass your code directly into gcc? For example: gcc -? 'int main(){return 0;}'

查看:52
本文介绍了您可以将代码直接传递到gcc吗?例如:gcc-?'int main(){return 0;}'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将代码直接传递到gcc吗?如果可以的话,它的命令行选项是什么?

Can you pass your code directly into gcc? If so what is the command line option for it?

例如:

g++ -? 'int main(){return 0;}'

我需要知道,因为我使用的是系统命令,而不是制作文件:

I need to know because I am using a system command and I rather not make files:

system("g++ -C "+code_string+" -o run.out");

Basile Starynkevitch解决方案有效,但是使用换行符时出现编译错误:

Basile Starynkevitch solution worked, however I am getting compile errors when I use newlines:

echo '#include\nint main(){printf("Hello World"); return 0;}' | g++ -x c++ -Wall -o myprog /dev/stdin

已修复

echo -e '#include\nint main(){printf("Hello World"); return 0;}' | g++ -x c++ -Wall -o myprog /dev/stdin

推荐答案

您可以要求GCC读取stdin.阅读其文档的调用GCC 章节.将其 -x 选项/dev/stdin 或带有-:

You could ask GCC to read from stdin. Read the Invoking GCC chapter of its documentation. Use its -x option with /dev/stdinor with -:

 echo 'int main(){return 0;}' | g++ -x c++ -O -Wall -o myprog /dev/stdin

顺便说一句,由于 int main(){return 0;} 是有效的C程序,因此可以使用

BTW, since int main(){return 0;} is a valid C program, you could use

 echo 'int main(){return 0;}' | gcc -x c -O -Wall -o myprog -

以编程方式,您应该考虑使用 popen(3)获取FILE * 句柄> pipe(7)(因此 FILE * f = popen("g ++ -x c ++ -O -Wall -o myprog/dev/stdin","w"); 然后检查 f 不为空),然后将 fprintf 放入其中,然后最后将其 pclose 插入.不要忘记测试 pclose 的状态.

Programatically, you should consider using popen(3) to get a some FILE* handle for a pipe(7) (so FILE* f = popen("g++ -x c++ -O -Wall -o myprog /dev/stdin", "w"); then check that f is not null) and fprintf into it then pclose it at last. Don't forget to test the status of pclose.

但是,GCC花费的大部分时间不是解析(使用-ftime-report 开发人员选项以进行查找).您经常会要求它优化(使用 -O2 -march = native 或仅 -O ),您肯定会要求提供所有警告(至少带有 -Wall ,甚至还有 -Wextra ).

However, most of the time spent by GCC is not parsing (use -ftime-report developer option to find out). You often want to ask it to optimize (with -O2 -march=native or just -O for example), and you surely want to ask for all warnings (with at least -Wall and perhaps also -Wextra).

如果您想在中生成一些插件代码/tmp/someplugin.so /tmp/myemitted.cc 中发出的一些C ++代码中,成为在Linux上动态加载,将其编译为与位置无关代码到带有例如

If you want to produce some plugin code in /tmp/someplugin.so from some emitted C++ code in /tmp/myemitted.cc to be dynamically loaded on Linux, compile it as position-independent code into a shared object dynamic library with e.g.

g++ -o /tmp/someplugin.so -fPIC -shared -Wall -O /tmp/myemitted.cc

等....,然后使用 dlopen(3)/tmp/someplugin.so 上的a>> dlsym(3)来获取一些已加载的符号.我的 GCC MELT 正在执行此操作.

etc.... then use dlopen(3) on /tmp/someplugin.so with dlsym(3) to fetch some loaded symbols. My GCC MELT is doing this.

由于解析时间可忽略不计,因此您可以在一些临时文件(在/tmp//run 内)中编写C或C ++代码,而这些文件通常是快速的 tmpfs ,因此写入它不需要磁盘I/O.

Since parsing time is negligible, you could instead write C or C++ code in some temporary file (inside /tmp/ or /run which is often some fast tmpfs on most Linux systems, so writing into it does not require disk I/O).

最后,最近的GCC(至少使用 GCC 6 )也具有 GCCJIT (实际上是libgccjit).您可以使用它构建生成的代码的某种表示形式,然后要求GCC对其进行编译.

At last, recent GCC (use at least GCC 6) also has GCCJIT (actually libgccjit). You could use it to build some representation of generated code then ask GCC to compile it.

另请参见此和 C ++ dlopen mini howto 如何编写共享库

See also this and that. Read the C++ dlopen mini howto and the Program Library HowTo, and Drepper's How To Write Shared Libraries

我宁愿不制作文件

I rather not make files

生成临时文件(请参见 mkstemp(3)等...,您实际上还可以在/tmp/下以 .c 结尾的一般随机文件名,然后在 AST 在从中发出C ++或C代码之前).并使用一些 Makefile 来编译带有某些> make 命令的优点(对于高级用户)能够更改编译器或选项(通过编辑该 Makefile 以配置 make ).

Generating a temporary file (see mkstemp(3) etc... and you practically could also general some random file name under /tmp/ ending with .c, then register its removal with atexit(3) passed some function doing unlink(2)...) is really quick (but you should build some kind of AST in memory before emitting C++ or C code from it). And using some Makefile to compile the generated code with some make command has the advantage (for the advanced user) to be able to change compilers or options (by editing that Makefile to configure make).

因此,恕我直言,避免使用临时文件是错误的(请注意, gcc g ++ 也在生成和删除临时文件,例如包含一些汇编代码).相反,我建议使用一些随机数生成一个临时文件(匹配/tmp/mytemp*.cc )(请参见

So you are IMHO wrong in avoiding temporary files (notice that gcc & g++ are also generating and deleting temporary files, e.g. containing some assembler code). I would suggest on the contrary generating a temporary file (matching /tmp/mytemp*.cc) using some random numbers (see random(3); don't forget to seed the PRNG with e.g. srandom(time(NULL)); early in your main). It could be as simple as

char tmpbuf[80];
bool unique;
do { // in practice, this loop is extremely likely to run once
  snprintf(tmpbuf, sizeof(tmpbuf), "/tmp/mytemp_%lx_p%d.cc", 
           random(), (int)getpid());
  unique = access(tmpbuf, F_OK);
} while (unique);
// here tmpbuf contains a unique temporary file name

您编码:

system("g ++ -C" + code_string +"-o run.out");

system("g++ -C "+code_string+" -o run.out");

当心, + 通常不是字符串分类.您可以使用 snprintf(3) asprintf(3)构建字符串.或在C ++中使用 std :: string .如果使用 system(3),则应检查其返回代码:

Beware, + is usually not string catenation. You might use snprintf(3) or asprintf(3) to build strings. Or use in C++ std::string. And if you use system(3) you should check its return code:

char cmdbuf[128];
snprintf(cmdbuf, sizeof(cmdbuf), "g++ -Wall -O %s -o run.out", tmpbuf);
fflush(NULL);
if (system(cmdbuf) != 0) {
   fprintf(stderr, "compilation %s failed\n", cmdbuf);
   exit(EXIT_FAILURE);
}

顺便说一句,您的示例是错误的(缺少< stdio.h> ));它是C代码,而不是C ++代码.应该是

BTW, your example is wrong (missing <stdio.h>); it is C code, not C++ code. It should be

echo -e '#include <stdio.h>\nint main(){printf("Hello World"); return 0;}' \
     |  gcc -x c -Wall -O -o myprog -


PS.我的答案集中在Linux上,但是您可以将其调整为适合您的OS.

这篇关于您可以将代码直接传递到gcc吗?例如:gcc-?'int main(){return 0;}'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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