user3792326
user3792326

Reputation:

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?

Upvotes: 2

Views: 1300

Answers (4)

Clifford
Clifford

Reputation: 93476

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.

Upvotes: 0

Haja Maideen
Haja Maideen

Reputation: 468

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.

Upvotes: 0

2785528
2785528

Reputation: 5566

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 (i.e. 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.

Upvotes: 5

Matt Adams
Matt Adams

Reputation: 719

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.

Upvotes: 3

Related Questions