Setting up C++ Development Environment
Last Updated :
30 Jun, 2023
C++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features.
C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an environment to be set up on our local computer to compile and run our C++ programs successfully. If you do not want to set up a local environment you can also use online IDEs for compiling your program.
Using Online IDE
IDE stands for an integrated development environment. IDE is a software application that provides facilities to a computer programmer for developing software. There are many online IDEs available that you can use to compile and run your programs easily without setting up a local development environment. The
ide.geeksforgeeks.org is one such IDE provided by GeeksforGeeks.
You can click on the Run on IDE button to run the program.
C++
// Using online ide of C++
#include <iostream>
using namespace std;
int main()
{
cout << "Learning C++ at GeekforGeeks";
return 0;
}
OutputLearning C++ at GeekforGeeks
Time Complexity: O(1)
Auxiliary Space: O(1)
Setting up a Local Environment
For setting up a C++ Integrated Development Environment (IDE) on your local machine you need to install two important software:
- C++ Compiler
- Text Editor
1. C++ Compiler
Once you have installed the text editor and saved your program in a file with the '.cpp' extension, you will need a C++ compiler to compile this file. A compiler is a computer program that converts high-level language into machine-understandable low-level language. In other words, we can say that it converts the source code written in a programming language into another computer language that the computer understands. For compiling a C++ program we will need a C++ compiler that will convert the source code written in C++ into machine codes. Below are the details about setting up compilers on different platforms.
Installing GNU GCC on Linux
We will install the GNU GCC compiler on Linux. To install and work with the GCC compiler on your Linux machine, proceed according to the below steps:
A. You have to first run the below two commands from your Linux terminal window:
sudo apt-get update
sudo apt-get install gcc
sudo apt-get install g++
B. This command will install the GCC compiler on your system. You may also run the below command:
sudo apt-get install build-essential
This command will install all the libraries which are required to compile and run a C++ program.
C. After completing the above step, you should check whether the GCC compiler is installed in your system correctly or not. To do this you have to run the below-given command from the Linux terminal:
g++ --version
D. If you have completed the above two steps without any errors, then your Linux environment is set up and ready to be used to compile C++ programs. In further steps, we will learn how to compile and run a C++ program on Linux using the GCC compiler.
E. Write your program in a text file and save it with any file name and.CPP extension. We have written a program to display "Hello World" and saved it in a file with the filename "helloworld.cpp" on the desktop.
F. Now you have to open the Linux terminal and move to the directory where you have saved your file. Then you have to run the below command to compile your file:
g++ filename.cpp -o any-name
G. filename.cpp is the name of your source code file. In our case, the name is "helloworld.cpp" and any-name can be any name of your choice. This name will be assigned to the executable file which is created by the compiler after compilation. In our case, we choose any-name to be "hello".
We will run the above command as:
g++ helloworld.cpp -o hello
H. After executing the above command, you will see a new file is created automatically in the same directory where you have saved the source file and the name of this file is the name you chose as any-name.
Now to run your program you have to run the below command:
./hello
I. This command will run your program in the terminal windows.
2. Text Editor
Text Editors are the type of programs used to edit or write texts. We will use text editors to type our C++ programs. The normal extension of a text file is (.txt) but a text file containing a C++ program should be saved with a '.cpp' or '.c' extension. Files ending with the extension '.CPP' and '.C' are called source code files and they are supposed to contain source code written in C++ programming language. These extension helps the compiler to identify that the file contains a C++ program.
Before beginning programming with C++, one must have a text editor installed to write programs. Follow the below instructions to install popular code editors like VS Code and Code::Block on different Operating Systems like windows, Mac OS, etc.
1. Code::Blocks Installation
There are lots of IDE available that you can use to work easily with the C++ programming language. One of the popular IDE is Code::Blocks.
- To download Code::Blocks, select the setup package based on your OS from this link - Code::Blocks Setup Packages.
- Once you have downloaded the setup file of Code::Blocks from the given link open it and follow the instruction to install.
- After successfully installing Code::Blocks, go to File menu -> Select New and create an Empty file.
- Now write your C++ program in this empty file and save the file with a '.cpp' extension.
- After saving the file with the '.cpp' extension, go to the Build menu and choose the Build and Run option.
2. XCode Mac OS X Installation
If you are a Mac user, you can download Xcode as a code editor.
- To download Xcode you have to visit the apple website or you can search for it on the apple app store. You may follow the link - Xcode for MacOS to download Xcode. You will find all the necessary installation instructions there.
- After successfully installing Xcode, open the Xcode application.
- To create a new project. Go to File menu -> select New -> select Project. This will create a new project for you.
- Now in the next window, you have to choose a template for your project. To choose a C++ template choose the Application option which is under the OS X section on the left sidebar. Now choose command-line tools from available options and hit the Next button.
- On the next window provide all the necessary details like 'name of organization', 'Product Name, etc. But make sure to choose the language as C++. After filling in the details hit the next button to proceed to further steps.
- Choose the location where you want to save your project. After this choose the main.cpp file from the directory list on the left sidebar.
- Now after opening the main.cpp file, you will see a pre-written c++ program or template provided. You may change this program as per your requirement. To run your C++ program you have to go to the Product menu and choose the Run option from the dropdown.
Another very easy-to-use and most popular IDE nowadays, is VSC( Visual Studio Code), for both Windows and Mac OS.
3. Installing VS Code on Windows
Start with installing Visual Studio Code as per your windows. Open the downloaded file and click Run -> (Accept the agreement) Next -> Next -> Next -> (check all the options) -> Next ->Install->Finish.
Now you'll be able to see the Visual Studio Code icon on your desktop.
- Download the MinGW from the link.
- After Install, "Continue". Check all the Packages (Right Click -> Mark for Installation). Now, Click on Installation (left corner) -> Apply Changes. (This may take time)
- Open This PC -> C Drive -> MinGW -> Bin. (Copy this path)
- Right, Click on "This PC" -> Properties -> Advanced System Setting -> Environment variables -> (Select PATH in System variables) -> Edit -> New -> Paste the path here and OK.
- Go to Visual Studio Code, and Install some useful extensions (from the right sidebar, last icon(probably))-
- Now, Go to Setting -> Settings -> Search for Terminal -> Go to the end of this page -> Check [ Code-runner: Run In Terminal ]
Yay! You are good to go now. Open any folder, create new files and Save them with the extension ".cpp".
4. Installing VS Code on Mac OS
First of all, Install Visual Studio Code for Mac OS using this link. Then We'll install the compiler MinGW. For this, we first need to install Homebrew.
To install Homebrew, Open Terminal (cmd + space). Write Terminal and hit Enter. In cmd copy the given command
arch -x86_64 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install)" < /dev/null 2> /dev/null
This will download and install HomeBrew on your Mac system. This process may take time.
Now We'll install the MinGW compiler on Mac OS. Paste the given command in the terminal and press Enter.
arch -x86_64 brew install MinGW-w64
- This is also time taking process so be patient!
- Go to Visual Studio Code, and Install some useful extensions (from the right sidebar, last icon(probably))-
- Now, Go to Setting -> Settings -> Search for Terminal -> Go to the end of this page -> Check [ Code-runner: Run In Terminal ]
Yay! You are good to go now. Now open any folder, create new files, and Save them with the extension ".cpp".
Similar Reads
C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why
5 min read
Introduction to c++
Difference between C and C++C++ is often viewed as a superset of C. C++ is also known as a "C with class" This was very nearly true when C++ was originally created, but the two languages have evolved over time with C picking up a number of features that either weren't found in the contemporary version of C++ or still haven't m
3 min read
Setting up C++ Development EnvironmentC++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an enviro
8 min read
Header Files in C++C++ offers its users a variety of functions, one of which is included in header files. In C++, all the header files may or may not end with the ".h" extension unlike in C, Where all the header files must necessarily end with the ".h" extension. Header files in C++ are basically used to declare an in
6 min read
Namespace in C++Name conflicts in C++ happen when different parts of a program use the same name for variables, functions, or classes, causing confusion for the compiler. To avoid this, C++ introduce namespace.Namespace is a feature that provides a way to group related identifiers such as variables, functions, and
6 min read
Writing First C++ Program - Hello World ExampleThe "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu
4 min read
Basics
C++ Data TypesData types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
C++ VariablesIn C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable
4 min read
Operators in C++C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.Example:C++#include <iostream> using namespace std; int main() { int a = 10 + 20; cout << a; return 0; }Outpu
9 min read
Basic Input / Output in C++In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.Output Stream: If the direction of flow of bytes
5 min read
Control flow statements in ProgrammingControl flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow St
15+ min read
C++ LoopsIn C++ programming, sometimes there is a need to perform some operation more than once or (say) n number of times. For example, suppose we want to print "Hello World" 5 times. Manually, we have to write cout for the C++ statement 5 times as shown.C++#include <iostream> using namespace std; int
7 min read
Functions in C++A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
C++ ArraysIn C++, an array is a derived data type that is used to store multiple values of similar data types in a contiguous memory location.Arrays in C++Create an ArrayIn C++, we can create/declare an array by simply specifying the data type first and then the name of the array with its size inside [] squar
10 min read
Strings in C++In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s
5 min read
Core Concepts
Pointers and References in C++In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a
5 min read
new and delete Operators in C++ For Dynamic MemoryIn C++, when a variable is declared, the compiler automatically reserves memory for it based on its data type. This memory is allocated in the program's stack memory at compilation of the program. Once allocated, it cannot be deleted or changed in size. However, C++ offers manual low-level memory ma
6 min read
Templates in C++C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
Structures, Unions and Enumerations in C++Structures, unions and enumerations (enums) are 3 user defined data types in C++. User defined data types allow us to create a data type specifically tailored for a particular purpose. It is generally created from the built-in or derived data types. Let's take a look at each of them one by one.Struc
3 min read
Exception Handling in C++In C++, exceptions are unexpected problems or errors that occur while a program is running. For example, in a program that divides two numbers, dividing a number by 0 is an exception as it may lead to undefined errors.The process of dealing with exceptions is known as exception handling. It allows p
11 min read
File Handling through C++ ClassesIn C++, programs run in the computerâs RAM (Random Access Memory), in which the data used by a program only exists while the program is running. Once the program terminates, all the data is automatically deleted. File handling allows us to manipulate files in the secondary memory of the computer (li
8 min read
Multithreading in C++Multithreading is a technique where a program is divided into smaller units of execution called threads. Each thread runs independently but shares resources like memory, allowing tasks to be performed simultaneously. This helps improve performance by utilizing multiple CPU cores efficiently. Multith
5 min read
C++ OOPS
Standard Template Library (STL)
Practice Problem