How I automated my C++ code compilation using NodeJS?

How I automated my C++ code compilation using NodeJS?

·

2 min read

Inspiration

I was doing a little competitive coding just to taste that side of computer science, although being a passionate web enthusiast, one thing that made me frustrated more than Dynamic Programming is, we need to compile the code and execute it every time you change your code and that drove me crazy

Solution

So out of the urge to solve this problem, I developed a CLI package that just does this, auto compiles your C++ code and I called it cpp-code-init

How it works

Prerequisites

  • Make sure that you have g++ compiler installed in your local machine
    • to check if g++ is installed enter g++ --version if installed you can see the version of g++, else you can install it from here
  • NPM minimum version 5.2+ is required

Installation

open a terminal and enter npx cpp-code-init and you will see something like this

2021-08-23 16_11_48-Windows PowerShell.png

  • move into the directory that has just been created by executing cd cpp-code-init in your command line
  • install the required packages by executing npm i in the same directory
  • change the code in main.cpp and save to see your changes in output.txt file

Folder structure

The contents generated by the CLI consists of 3 main files where we spend most of the time writing C++ code

  • main.cpp
  • input.txt
  • output.txt

As the name suggests your source code will be in main.cpp file and the input is taken from input.txt file and then it compiles and executes the code and the result is written in shown in output.txt file

Try changing the code in main.cpp and save the file and there you go with the compiled results in output.txt file 🥳

How I use my package

I generally use this tool to quickly solve some of the problems that've been in my mind and for some competitive programming questions too. I love to work on VS Code. Personally, I prefer using VS Code because it is so easy to set up a layout where you can see and change all three files, and I don't need to worry about executing the code because it will be handled as soon as I save it. This is how my VS Code looks when I am working with C++

2021-08-23 16_24_17-.png

Conclusion

It was a fun project through which I could solve some of my problems and automate the process of compilation. I hope this project will help you in reducing the mouse clicks

Here is the GitHub Repo of the code Please feel free to suggest some changes and improvements, that can be made to improve this tiny package