Command to run C++ Program in terminal
Command to run C++ Program in terminal
The C++ programming language is exceptional; it's widely used and incredibly powerful for all types of programming.
It is one of the most widely used programming languages and frequently ranks in the top two or three languages on any list.
How to write a C++ program and that runs in a terminal?
We may begin coding as soon as you download the Community Edition (or RAD Studio CE) of C++ Builder, as well as any Professional, Architect, or Enterprise editions of C++ Builder, as well as Dev-C++, VC++, or GNU C/C++.
- C++ Builder Community Edition is available for free download at https://www.embarcadero.com/products/cbuilder/starter
- Professional, Architect, or Enterprise editions of C++ Builder are available for usage by developers.
- Visit https://www.embarcadero.com/products/cbuilder for more information.
Afterward, you can run your software by following these instructions:
- Create a fresh project, such as a console application or solution.
- Start writing code right away using very basic syntax and save all project files, including cpp files.
- To compile your code, use the IDE's Run Button or Keys or a compiler.
Let's perform these steps in C++ Builder as an illustration.
To create a new C++ console application, use the File -> New -> Console Application - C++ Builder" menu option. Press OK to begin entering code in the next window.
1.Importing required libraries:
- The first thing you should do before beginning to code is to add the libraries you intend to utilise. Each library for the C++ programming language provides instructions or functions for carrying out particular tasks.
- The iostream library, for instance, offers common input and output techniques for displaying data and reading from files and other sources.
In most cases, the iostream header is sufficient for beginners to enable them to construct straightforward apps. This library header can be included as shown below.
#include <iostream>
2.Main Function:
- You have to provide a main procedure second. It is initially executed because it is the main portion of the programme, hence the name.
- All of your program's other components are, in the simplest terms, either directly or indirectly launched from the main portion.
- Once you start writing more complex programmes, things become a little more involved than that, but for the time being, you may think of the major piece of your programme code as the "main loop," which is where things start to happen.
int main()
{
}
3.Body of the Code:
- Then, in this procedure, you should enter your lines of programming code between the and brackets. If we are just starting out, the "Hello World" examples are excellent for learning the principles of a specific programming language.
- It is also a good idea to understand how to alter text, how the language's syntax (rules) work, how to compile and link, how to debug and execute, and how to deploy or release.
- The "Hello World" C++ example is shown in its full below and can be run using C++ Builder as well.
Here is a console application that uses the Hello World C++ example.
// importing the libraries which are required
#include <iostream>
// main function is written
int main()
{
// required body of the code
std::cout << "Hello World welcome !!\n";
system("pause");
return 0;
}
Explanation:
- In C and C++, you must provide a semicolon (";") after each command you use.
- In the previous example, we use the std::cout method to print a string and then use system("pause"); finally, we exit and return 0, indicating that our main app has ran successfully and has exited.
- The previously mentioned C++ code can be typed directly into the Dev-C++ code editor or it can be copied and pasted.
- We strongly advise writing the code yourself rather than copying and pasting it if you are a new learner. This will make it easier for you to comprehend how C++ functions and enable you to spot any early coding errors.
OUTPUT:
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal1.png)
How to utilise the RAD Studio Command Prompt to launch a C++ programme in the terminal?
You can launch RAD Studio Command Prompt if you create and save your C++ Builder C development project as stated above.
1. Ensure that C++ Builder or RAD Studio is installed, and that the project's C files are saved in a folder.
2. Write "RAD" on the Windows Key, then select "RAD Studio Command Prompt" from the Windows Menu. This will launch Command Prompt with the settings from RAD Studio.
3. To find the folder containing your project, use the CD command.
For Example,
- You can now save all of your project files to a folder after the coding is finished. To save all your files, select File -> Save Project As.
- After completing this step, you can use the Compile and Run buttons in the editor, hit F9 to compile and run, or F10 to execute your code. With this, "Hello World, Welcome!" will print. The C language is also supported by C++ compilers.
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal2.png)
- To view the files for your C project, enter dir command.
- Your project file is the cbproj file. To utilise the MS Build app, type msbuild. Your programme will be built using the project settings; it will make use of the appropriate C++ linker and compiler that are described in this project file.
For instance,
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal3.png)
The errors and warnings will be compiled and listed as seen in the sample below.
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal4.png)
- An output folder is where the executable file is generated. These folders include iOS, Android, Win32, and Win64, among others. Using the cd and dir commands, you can examine these folders.
- Additionally, depending on your project, the project may be under the Debug or Release folders. Our project was a 32-bit Windows Console Application (Win32), and the Win32Debug folder contained the project's debug files.
- So, as seen below, navigate to the Win32 and Debug folders.
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal5.png)
- You may see all output files in the Debug folder by typing the command, as shown in the example below.
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal6.png)
- At this point, you can run the executable *.exe file.
![Command to run C++ Program in terminal](https://static.tutorialandexample.com/cpp/command-to-run-cpp-program-in-terminal7.png)