如何在程序运行时生成和编译C ++代码? [英] How to generate and compile C++ code while the program is running?

查看:156
本文介绍了如何在程序运行时生成和编译C ++代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我需要的不是一个特定的代码(当然是伟大的),但只是一个想法和方法如何实现我的目标。



1)我必须在C ++中创建一个程序,它产生一个C ++代码的一个例子,这是每次有点不同。 (这将不会对我造成问题,我将使用一个模板并随机化一些代码中的变量,这将使它每次都是唯一的。)



2)生成的代码和用户将必须输入,他认为代码打印输出。



这里是问题开始的地方



3)我必须获取生成的代码,并以某种方式编译它,以获得一个包含程序打印输出的文本的字符串。



4)然后将字符串与用户输入的字符串进行比较。



因此,步骤3) t没有帮助我想出了一个.cpp文件的函数中生成的代码,然后调用该函数,但我不能得到它的工作,所以我开始想,我应该问

解决方案


  1. 撰写.cpp文件

  2. 调用编译器(或make或等效编译系统)

  3. 如果编译失败,可能显示错误?

  4. 如果编译成功,运行生成的程序并显示输出

4转换为脚本,并使用 system popen 调用它。脚本可以确保文件名是唯一的,将stderr折叠到stdout等等。



你正在运行的程序不是真正地与编译代码交互,只是阅读输出,所以保持它作为一个单独的过程可能是最简单的。脚本可以添加一些标记,以帮助您区分编译器输出/错误与程序输出。






一个批处理文件多年,但一旦你知道如何从命令行运行编译器( ref ),你可以写一个脚本到:


  1. 编译 .cpp 文件

  2. 执行生成的 .exe

  3. 将其输出重定向到文件

然后,在C ++中,您只需将代码保存到 .cpp 脚本期望的文件,执行 system(myScript.bat)的脚本,然后读取输出文件。 p>

如果你不想编写一个单独的批处理脚本,你可以调用 system 一次来调用编译器,并再次执行生成的 .exe


So basically what I need isn't a specific code (of course that would be great), but just an idea and methods on how to achieve my goal.

1) I have to create a program in C++ , which generates a little example of C++ code, that is each time a bit different. (This causes no problems for me, I will use a template and randomize some variables in the code, which will make it unique every time.)

2) I will display the generated code and the user will have to type in, what he thinks the code prints out.

And here is where the problems start:

3) I have to take the generated code and compile it somehow to get a string with the text that the program would have printed out.

4) And then compare the string with what the user has typed in.

So the step 3) is where I stop and can't figure it out without help... I was thinking to write the generated code in a function of a .cpp file and then call that function, but I couldn't get it to work, so I started to think, I should ask an expert, maybe there are some other methods or ideas how to achieve this.

解决方案

  1. Write the .cpp file
  2. invoke the compiler (or make or equivalent build system)
  3. if compilation fails, presumably display the errors?
  4. if compilation succeeds, run the resulting program and display the output

It's probably simplest to wrap 2/3/4 into a script, and invoke that with system or popen. The script can make sure the filenames are unique, fold stderr into stdout, etc. etc.

Your running program isn't really interacting with the compiled code, just reading the output, so keeping it as a seperate process is probably easiest. The script can add some markup to help you distinguish compiler output/errors from the program output.


I haven't written a batch file for years, but once you know how to run your compiler from the command line (ref), you can write a script to:

  1. compile a .cpp file
  2. execute the resulting .exe
  3. redirect its output to a file

then, in C++ you just need to save the code to the .cpp file the script expects, execute the script like system("myScript.bat"), and then read the output file.

If you don't want to write a seperate batch script, you can just call system once to invoke the compiler, and again to execute the resulting .exe.

这篇关于如何在程序运行时生成和编译C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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