简体   繁体   中英

Is There a C++ Command Line?

So Python has a sort of command line thing, and so does Linux bash (obviously), and I'm sure other programming languages do, but does C++? If not, why do C++ scripts have to be compiled first and then run?

If not, why do C++ scripts have to be compiled first and then run?

C++ code does not need to be compiled to be run. There are interpreters.

The reason most of us prefer compiled C++ is that the resulting executable is 'faster'.

Interpreted computer languages can do extra things to achieve similar performance (ie just-in-time compile), but generally, 'scripts' are not in the same league of fast.

Some developers think not having to edit, compile, link is a good thing ... just type in code and see what it does.

Anyway, the answer is, there is no reason that C++ "has to" be compiled. It is just the preferred tool for most C++ developers.

Should you want to try out C++ interpreters, search the net for CINT, Ch, and others.

Indeed there are interpreters for C++ that do what you want. Check out Cling .

To the commenters saying C++ can't have interpreters because it's a compiled language: yes, typically you use a compiler with C++. But that doesn't mean it's impossible to write an interpreter for it.

There is no command lines to run C++ instructions. It is compiled first and then target machine code generated (intermediate obj code, and linked) to run.

The reason is, It is matter of language design for various considerations like performance, error recovery etc. Compiled code generate target machine code directly and run faster than interpreted languages. Compiled code take program as whole and generate the target machine code vs interpreted code take few instruction at once. Interpreted language require intermediate programs to target the final machine code, so it may be slow.

In nutshell, it is language design evolution. When first computers appeared programming is done directly in machine language. Those programs run instruction by instruction. Later high level language appeared, where machine language is abstracted with human friendly instructions and compilers designed to generate equivalent machine code.

Later Computer program design advanced, and CPU instruction cycle speed increased, we could afford intermediate interpreters for writing safer programs.

Choice is wider now, earlier performance centric apps demanded compiled code. Now even interpreted code equally faster in common use cases.

While there are interpreters for C++ -like languages, that is not really the point; C++ is a compiled language that is translated to native machine code. Conversely scripting languages are (typically) interpreted (albeit that there are also compilers for scripting languages to translate them to native code).

C++ is a systems-level capable language. You have to ask yourself - if all languages ran in a shell with a command line and were interpreted, what language is that shell or interpreter, or even the OS they are running on written in?

Ultimately you need a systems level language, and those are most often C, C++ and assembler.

Moreover because it is translated to machine level code at compilation, that code runs directly and stand-alone without the presence of any interpreter, and consequently can be simpler to deploy, and will execute faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM