Execute C++ in Google Colab

Google Colab is a platform that enables the executing Jupyter Notebook(.ipnb). Without even having a powerful computer, you can do heavy computations with it. For example, I have done scientific computation from my phone, just entered the site and typed some codes like texting my friends. That was easy, as long I know the governing equations of the system. Anyway, let’s take a look at how to execute C++ on your Colab.

Jupyter notebooks are connected to a virtual linux machine, whenever you type the interface some expressions, starting with !, it is executed in Linux terminal, instead of Python Core Interpreter.

If you know how to execute C++ in linux terminal, go on, just apply the same scheme as you do in other other devices. Rest of the content is for those how not to do it, so let’s move on.

Write this python code to struct C++ file and save it. Insert the the C++ in a Python doc-string then save it

Cpp=”””
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout<<cos(5)<<“\\n”;
return 0;
}

“””

with open(“file.cpp”,”w”) as file:
file.write(Cpp)
file.close()

Now it is time to compile the file

!g++ file.cpp -o foo

And execute it with

!./foo

Here is the image of the process

C++ in Google Colab

Super easy, as you can see, I have used my phone for programming this

Have a nice programing!!