有没有 C++ 这样的东西?口译员?
嗨,
我目前正在学习 C++,并且总体上是编程的初学者。我一直在尝试编写一些代码来解决我正在使用的书中的一些编程问题。我发现我经常在编写的内容中犯错误,并且这些错误在程序运行时出现。当有常规输出时,通常很明显我在程序中哪里出错了。但在长时间的计算中,我常常不确定为什么特定代码会以某种方式运行。我最近也看了Python。 Python 与解释器一起工作,它可以获取任何 Python 代码并计算其输出。
我想知道 C++ 是否有类似的东西。现在,当我想检查一行或一行代码时,我必须注释掉很多,保存它,编译它,然后从命令行运行它。我必须为一个错误重复多次,直到解决它。有没有一种方法可以在活动终端中键入代码来运行代码并显示输出?更好的方法是在 IDE 中选择一个代码块(就像选择文本)或多个代码块(以查看如何处理函数),然后单击运行以仅运行该代码块并查看其输出而无需注释掉不相关的行或保存文件。编译后的代码可以仅驻留在内存中。
Possible Duplicate:
Have you used any of the C++ interpreters (not compilers)?
Hi,
I am currently learning C++ and a beginner in programming in general. I've been trying to write some code to a few programming problems from the book I'm using. What I find is that often I make mistakes in what I write and those mistakes come up when the program is run. Its usually quite obvious where in the program I've gone wrong when there is regular output. But in a long computation I'm often not sure why a particular code has acted a certain way. I've also looked at Python recently. Python works with an interpreter, which can take any piece of Python code and compute its output.
I was wondering if there was something similar for C++. Right now when I want to check a line or block of code I have to comment out a lot, save it, compile it, and then run it from a command line. And I have to do that many times for a single error until I've solved it. Is there a way to type code into an active terminal which would run code and show me output? What would be better still would be a way to select a block of code (like you select text) or multiple blocks (to see how a function is being handled) within the IDE and click run to run just that block of code and see its output without having comment out irrelevant lines or to save the file. The compiled code could just reside in memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
CINT 是 ac & C++ 解释器接受几乎所有有效的 C++。不幸的是,许多 Linux 发行版不提供它,您可能必须从源代码构建它......这是一项艰巨的任务。
CINT is a c & C++ interpretter that accepts nearly all valid C++. Unfortunately many Linux distros do not offer it, and you'll probably have to build it from source... and that is a non-trivial task.
通常,调试器用于从选定的断点开始逐行单步执行代码,并监视所有变量/值。
单元测试是一种测试较小代码片段的技术。
Typically a debugger is used to step through code line by line, starting at a chosen breakpoint, and keep watch of all variables/values.
Unit testing is a technique to test smaller pieces of code.
大多数 IDE 中都有的步进调试器可以帮助您完成此任务。
这里(例如)关于如何在 Visual Studio 中设置执行点的描述,这听起来像是您想要做的。
对于某些情况,“立即窗口” 可能对你有用。它允许您输入表达式以立即计算。
您可以使用调试器运行到感兴趣的点(您将在其中设置断点),而不是仅仅独立运行单独的行,或者依靠打印语句来告诉您决定打印的任何变量的状态,然后您可以检查任何范围内变量的状态,甚至可以改变程序的正常流程。
A stepping debugger, as found in most IDEs will help you with this.
Here (for example) is a description of how to set the Execution point in In Visual Studio, which sounds like what you want to do.
For certain situations, the "Immediate Window" may be of use to you. It allows you to type in expressions to evaluate immediately.
Rather than just running individual lines independently, or relying on print statements to tell you the state of whatever variables you have decided to print, you can use the debugger to run to the point of interest (where you will have set a breakpoint), then you can examine the state of any in-scope variables, or even alter the normal flow of the program.
有一些解决方案尝试做到这一点 - 我知道的解决方案是 Ch 和 TextTransformer。
然而,我怀疑这是否有效。 C++ 根本就不是为了作为解释性语言运行而设计的。
问题之一是 C++ 非常非常难以解析。这使得提供其他语言常用的某些类型的工具变得非常困难。例如,我认为没有任何 C++ 重构工具真正有效。
There are some solutions that try to do this - the ones I know are Ch and TextTransformer.
However, I doubt that this works very well. C++ is not at all designed to run as an interpreted language.
One of the problems is that C++ is very, very hard to parse. And this makes it very hard to provide certain types of tools that are usual for other languages. For example, I don't think there is any C++ refactoring tool that really works well.
C++ 是一种编译语言,与 python 不同。但市面上很少有 c/c++ 解释器,但不确定它们的功能。查看这些:Ch 解释器 和 CINT
如果您真的想学习 c++,请不要使用 c/c++ 解释器。
C++ is a compiled language not like python. But there are few c/c++ interpreters out there but not sure about their features. Check these out: Ch interpreter and CINT
If you really want to learn c++ please do not use the c/c++ interpreters.
如果您坚持使用交互式解释器,那么长期以来 CINT 就是ROOT 项目中使用的默认解释器。这些年来它变得越来越好,但在处理模板时仍然只有有限的功能。此外,还有一个举措,将其替换为 基于 clang 的 JIT 编译解释器< /a> 在 ROOT 项目内。
如果我是你,我会学习如何运行编译器和交互式调试器,就像一些评论中建议的那样。
If you insist on using a interactive interpreter there is since a long time CINT which is the default interpreter used in the ROOT project. It got better over the years, but still has only limited capabilities when dealing with templates. Also, there is a move to replace it with a JIT compiling interpreter based on clang inside the ROOT project.
If I were you I would learn how to run compiler and an interactive debugger like suggested in some comments already.