从字符串变量执行C ++ [英] execute C++ from String variable
本文介绍了从字符串变量执行C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中可以从字符串变量执行C ++代码。
Like in Javascript:
it is possible in C++ to execute the C++ code from string variable. Like in Javascript:
var theInstructions = "alert('Hello World'); var x = 100";
var F=new Function (theInstructions);
return(F());
我想要一个非常类似于C ++中的Javascript的东西。如何做?
I want something very similar like Javascript in C++. How to do that ?
推荐答案
您需要调用编译器来编译代码。此外,您将需要生成一些代码以将字符串包装在函数声明中。最后,你将不知何故需要加载编译的代码。
You will need to invoke a compiler to compile the code. In addition, you will need to generate some code to wrap the string in a function declaration. Finally, you'll then somehow need to load the compiled code.
如果我这样做(我不),我会:
If I were doing this (which I would not) I would:
- 在代码周围连接标准封装函数头
- 通过命令行调用编译器
system()
)在Windows或库(
.dll
> .so - 调用函数
- Concatenate a standard wrapper function header around the code
- Invoke a compiler via the command line (
system()
) to build a shared library (.dll
on windows or.so
on linux) - Load the shared library and map the function
- Invoke the function
在大多数情况下,这不是你想要编写C代码的方式。
This is really not the way you want to write C code in most cases.
这篇关于从字符串变量执行C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文