How to run c++ code/program in linux terminal
It is easy to compile and run a c++ program in terminal ( i am using ubuntu 18.4). Since you have come this far, I am assuming you have the basic knowledge creating a file and saving it in the Linux directory.
Rest steps to compile and run are mentioned below.
If you are just looking for the commands here they are
1.) g++ FILE_NAME.cpp -o OUTPUT_NAME
2.) ./OUTPUT_NAME
Lets take a simple example of c++ code.
#include<iostream> #include<math.h> #include<time.h> #include<string.h> #include<algorithm> #include<stdio.h> #include<stdlib.h> using namespace std; int main(){ cout<<"I AM AWESOME"; // "Hello Word" is way too main stream return 0; }
Saved as filename “example.cpp” at path “/bitsofjarvis/c++/example.cpp“.
I am in my home directory i.e “/home”.
Step 1: Make sure gcc or g++ compiler is installed. check the version by "gcc --version"
or Use the below command to install.sudo apt-get install build-essential
Step 2: Compile the code and create another executable binary file (by the name provided). Command is g++ FILE_NAME.cpp -o OUTPUT_NAME
Here in our example, it would be g++ /bitsofjarvis/algorith/example.cpp -o /bitsofjarvis/algorith/example_executable
One can see then a star marked file with name “*example_executable” would have been created.
step 3: Run the executable file to get the output of the code.
./PATH_TO_EXECUTABLE_FILE./bitsofjarvis/algorith/example_executable
That’s it you can see the output on the terminal :
I AM AWESOME
Please comment below, if I have missed anything or you need more clarity on any part. I woud be happy to answer it.
Happy Coding !!!!!