“评估功能”在 C++
我想知道 C++ 中是否存在像 Matlab 那样的“评估”函数。
实际上,我需要一个可以像命令行一样解释字符串的函数。
感谢您的回答。
I want to know if exists an "evaluate" function in C++ like the Matlab one.
In practise, I need a function that can interprets a string like a command line.
thanks for the answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您实际上尝试在正在运行的 C++ 应用程序中“评估”C++ 源代码,那么基本上不行——这不是该语言指定的功能。
C++ 子集(例如 CInt、Ch 和 UnderC)有解释器 - 如果您的 C++ 程序相对简单,它们也许能够运行它。或者,某些可以嵌入到已编译的 C++ 程序中,以允许某些运行时源代码评估,但对预编译代码及其变量的访问和更改能力有限。
正在运行的程序也可以调用编译器并动态加载/链接生成的库,但这是一种非常不寻常的做法,并且并非没有性能、安全性和互操作性问题:
system()
、exec()
、unlink()
调用之类的语句,滥用网络连接、滥用不必要的 CPU/内存/描述符等。如果您实际上需要更有限的东西,例如评估数学表达式或逻辑谓词的能力,可能以 C++ 源代码风格表示,也许读取或设置一些值,那么各种更有限和专门的库和嵌入式解释可用。甚至还有用于创建此类解析器的库,例如 boostspirit 库。
最后,其他语言(Lua、Ruby、Python、Perl、TCL 等)的解释器可以嵌入到 C++ 应用程序中,从而实现互操作性和安全性的各种方法。
If you are actually trying to "evaluate" C++ source code within a running C++ application, then basically no - it's not a feature specified by the language.
There are interpreters for subsets of C++ (e.g. CInt, Ch and UnderC) - they may be able to run your C++ program if it's a relatively simple one. Alternatively, some can be embedded within a compiled C++ program to allow some run-time source code evaluation, but with limited access to and ability to change the pre-compiled code and its variables.
It's also possible for a running program to invoke the compiler and dynamically load/link a resultant library, but this is a very unusual practice and not without performance, security and interoperability issues:
system()
,exec()
,unlink()
calls, abuse network connectivity, chew unwarranted CPU/memory/descriptors etc.If you actually need something more limited, like the ability to evaluate mathematical expressions or logical predicates, possibly expressed in a C++-source style, perhaps reading or setting some of your values, then various more limited and specialised libraries and embedded interpreted are available. There are even libraries for creating such parsers, such as the boost spirit library.
Finally, interpreters for other languages - Lua, Ruby, Python, Perl, TCL etc. - may be embedded in the C++ application, sporting various approaches to interoperability and security.
您可以使用
system()
:http://linux.die.net /man/3/系统You can use
system()
: http://linux.die.net/man/3/system