Objective>>

To create and execute a Cpp file and be able to run it from a command line.

I will explain how to do the whole process step by step in a short video.

First let me introduce you what a command line is:

“It is known as the line on the display screen where a command is expected. Generally, the command line is the line that contains the most recently displayed command prompt.”

Knowing that, we will be able to start writing codes.

I will use two programs to do it.

Atom; which is a Program designed for writing codes and save them on your computer.

Cygwin; which is the program that will compile and run the codes.

First!

We are going to create a new file in Atom and save it. It doesn’t matter how you want to call it, as long as it has the extension .cpp at the end, so Atom can identify it as a code and Cygwin too.

Once you have your new file.cpp we will start writing the codes.

Basically, we will tell our program to print on screen a sentence, in this case it will be the traditional “Hello World”.

In order to do that, we will use Values, which are defined as “A letter or a number or any other thing that can be store in a variable”.

The Value that aloud the user to input words in a really simple way is “Cout

So, our code will be like this:

#include <iostream>
using namespace std;

int main(){
  cout<<"Hello World"<<endl;

  return 0;
  
}

Once we have our program ready to compile, we have to do the following.

  1. Go to Cygwin and tell the computer where our file.cpp is.

For example, if we saved our file.cpp in a folder called “programacion1”, then we will have to input Cygwin the route of “programacion1” from “C” until we reach the folder were our file is.

(We have to put “cd” first every time that we want to look for a folder or a file, then we type “cygdrive” which commands the program to change to a new route.

So, we will do something like this:

cd /cygdrive/c/Users/User/Documents/Folder

 

Now that we told Cygwin where do we want him to look, now it’s time to compile and run the program that we just did: file.cpp

All we have to do is type:

g++ file.cpp

 

With this command, we are telling Cygwin that we want him to compile our file.

And after we click enter, if our codes do not present any bug or error, then NOTHING WILL HAPPEN.

This is important, if nothing happens, please don’t freak out, it means that our program was able to compile properly.

Now, all we have to do is order Cygwin to execute it, typing the following:

./a.exe

OR

ls (This will show us the content of the folder were our file is, and all we have to do later is type ./a.exe)

 

And that’s it, Cygwin will print on screen the “Hello World” that we wrote on Atom using “Cout”.

And Here is the video of the whole process and if you have any questions do not hessitate and ask me! 😀

Clic here to see the video!

 

CC BY 4.0 Ability to create a C++ file and run from command line. by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.