How To Read This Guide
This guide is meant for beginners as well as readers with basic knowledge. Most of the technical terms have been explained or have been linked to a webpage with more information.
If you still don't understand a term, just Google it :)
This guide explains how to install as well as use a C/C++ compiler to run your code. The compiler used here is GCC.
To write and edit code, any text editor such as Notepad can be used, although, a code editor is recommended because of syntax highlighting, code completion, and much more. Here, we will be setting up a popular cross-platform code editor - Visual Studio Code.
This guide has separate sections for different operating systems. Use the Table of contents to jump to the relevant section corresponding to the operating system of your interest.
NOTE: You are supposed to enter and run a command in a terminal whenever you are given one, in this blog.
A Terminal generally refers to:
Command Prompt on Windows. Windows also has Powershell and the new Windows Terminal which combines all the different shell environments.
Windows
Here, the native Windows version of GCC - called MinGW - will be installed.
There are quite a few methods to achieve this. We will be looking into two of them - one being the most widely popular; using the MinGW installer directly from its website - other being the simplest; using the Chocolatey package manager.
Use the Table of contents to jump to the method of your choice.
Using MinGW installer
Download the installer file
mingw-get-setup.exe
from https://osdn.net/projects/mingw/releases/.Run the
.exe
file. A window titled "MinGW Installation Manager" will open in a few minutes. In the Basic Setup tab, right-click the option namedmingw32-gcc-g++-bin
and click onMark for Install
.Then in the top left corner click on
Installation > Apply Changes
. Continue through the rest of the installation process while keeping all settings as default. And then, you wait...
GCC is installed! Now we have to make it accessible by adding it to the PATH:
Go to
Windows Explorer > Right-click on "This PC" > Properties > Advanced system settings > Environment Variables
.At the bottom System Variables panel, look for a Variable named PATH and double-click on it.
Click the New button to add a new path to Windows PATH.
Enter the Path of the folder to which you installed the GCC (by default, it's
C:\MinGW\bin
)Press OK until you are out of the menus.
You should now have the GCC installed and ready to be used! To verify, type gcc --version
in Command Prompt and press Enter
; if you get an output stating its version, then it was successfully installed.
The easier way (via chocolatey)
Chocolatey is a package manager for windows that makes installing applications a lot easier and more automatable. As opposed to the long process we went through in the above way, via chocolatey the entire process of installing chocolatey can be reduced to just 3 commands - executed in an Administrator Powershell:
Install chocolatey with the instructions from their website: https://chocolatey.org/install
Run
Get-ExecutionPolicy
in an administrator PowerShell. If it returns Restricted, then runSet-ExecutionPolicy AllSigned
.Now run:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
and wait for the installation of chocolatey to be complete.
Close and reopen the admin PowerShell session.
Run
choco install mingw
and typeA
to confirm the installation.You should see this if it succeeds
Now run
gcc --version
to verify that it has been installed properly.
Linux
Most Linux distributions will have GCC pre-installed. To check if it is installed:
gcc --version
If you get some output; NOT an error, then it is installed.
On Ubuntu and other Debian-based distributions: Run the following commands in the Terminal:
sudo apt update
sudo apt install build-essential
For other distributions just install the package
build-essential
from your package manager.
MacOS
Though GCC "appears" to be installed in MacOS by default, it's clang in disguise!
Why bother?
For the audience one of the main drawbacks of using clang can be not able to use <bits/stdc++. h>
, for your work. To know about the differences in-depth - refer to:
Getting GCC installed on MacOS
Install homebrew - ignore if already present.
To check whether it is installed or not use the following command
command -v brew
If the output is like the following then brew is installed!
~> command -v brew /opt/homebrew/bin/brew
Installing GCC.
brew update brew upgrade brew install gcc
Note
How GCC is installed asgcc-12
andgcc
is still clang.Now, you can either use
gcc-12
instead ofgcc
. But if you wantgcc
to invoke the GNU version of GCC instead of clang then execute the following lines in your terminal. Since MacOS comes withzsh
by default we will be giving command for it only but if you have changed your shell then you must be equipped enough to do the following steps on that shell too - Right?echo "alias gcc=/opt/homebrew/bin/gcc-12" >> ~/.zshrc
Unprecendeted issue with MacOS 13(Ventura) & gcc-12
If you are using MacOS Ventura and facing this issue - which means it's not fixed yet - no problem, then read the following issue/discussion to solve it!
Yes. You are going to read all of that, try to understand; try to fix. Repeat until you succeed! You have to start self-learning sometime later, so why not start now?
Compiling the code manually
Follow these steps to compile your .c
file manually.
Create a file with the name
first_c_program.c
with the following content in it and then save it.#include <stdio.h> int main(){ printf("Hello World!"); return 0; }
⚠️ Make sure you don't have spaces in your filename!
Compile the program and execute it. You can replace
executable.exe
withanything.anything
.For Windows users, the command to run will be the following, the above works for Linux & MacOS only.
.\filename.exe
Setting Up VS Code
Visual Studio Code is a free and open-source code editor that is customizable through extensions. The C/C++ extension can be used for compiling and running C/C++ code along with many more features.
Installing
Open VS Code
Open the extensions panel using the button on the left or by pressing
Ctrl+Shift+X
.Search and install the C/C++ extension.
Once the extension is installed, creating or opening any .c
or .cpp
file will have syntax highlighting, code completion, and other features.
Compiling
To compile the code, open a file and run Terminal > Build Task
from the main menu or press Ctrl + Shift + B
.
On Windows: From the dropdown, select C/C++: g++.exe build active file
(for C++ files) or C/C++: gcc.exe build active file
(for C files).
On Linux and MacOS: From the dropdown, select C/C++: g++ build active file
(for C++ files) or C/C++: gcc build active file
(for C files).
Running
Once the code has been compiled, an executable file will be created (with the same name as the code file). On Windows, this file will have a .exe
extension on Windows and no extension on macOS and Linux.
To run the file:
Open the VS Code integrated terminal by pressing Ctrl + `.
Type:
On Windows:
.\
followed by the name of the executable file.On Linux and macOS:
./
followed by the name of the executable file.
and press enter.
- The output of the code will be visible in the terminal!
Code Runner
Depending on your OS, we suggest different code runners. Hop into the section of your interest using the Table of Contents.
Windows 11
You are suggested to use code-runner using WSL instead of using this buggy
.run
extension which will be installed in this section ahead - which is shared just as an alternative that is relatively easy to set up.
Click on the extension's icon.
Search for
code runner
.Install the extension shown in the screenshot below.
Linux and MacOS
For simplicity and to automate a few steps for a list of languages you can try code-runner.
Installation
Copy and paste the following command in your terminal and wait for the final message - you are done!
curl https://raw.githubusercontent.com/proffapt/code-runner/main/setup.sh | /bin/bash
Can help in the following ways:
Just one command to compile and execute the compiled file:
run filename.extension
.Cleans compiled binary by default. So, that you don't submit binary instead of a
.c
file in your assignments.Detects the version of python to be used ( accuracy of 95% ).
Can be used to create a file with spaces in its name ( vs-code runner extension lacks this )
Can be used to execute a file in a different directory ( vs-code runner extension lacks this; and limits some functionalities )
Inbuilt debugger support.
Integrates with NeoVIM and VS-CODE - steps in the documentation of the tool.
And many more, read the documentation and code for a thorough understanding at https://github.com/proffapt/code-runner.
Tips and Further Reading
DO NOT GUESS how a piece of software might work.
When using a tool for the first time, you CAN'T and MUSTN'T guess how it works! Beginners often make this mistake and waste a lot of their time and of others too; without even searching or reading the docs!
Rather read the documentation/help for that program or just simply search for whatever you wanna achieve with that tool.
Learn to search!
This is a starting guide, so it was a bit explanatory. What we promote is self-learning. The basic idea behind it is very simple:
Read the error carefully and try to understand it.
Phrase your problem/goal.
Search it on your favourite search engine before asking in groups.
Either you will get the solution, if not then ask in groups/forums.
Other Authors
Harsh Khandeparkar (@harshkhandeparkar)
Jeffrey Samuel (@signor-koala)