Environment Setup
For setting up your own personal development environment on your local machine you need to install two important softwares.
- IDE or Text Editor:-
To write the C++ code we must have to install a text editor or IDE. We can use a simple text editor like Notepad++ to write the code and
save the file with an extension of '.cpp' or '.c'. This will be our source code files. And further, we can also use '.h' extension for creating the
header files.
Other than Notepad++ we can also use the IDE like Microsoft Visual Studio Code, which will provide many more options while writing the
code. Here we can also easily manage the directory structure and properly manage multiples files in our projects - Compiler:-
After writing the code next we will need to compile the same using the C++ compiler. A compiler is nothing but a computer program that
converts the high-level language into machine-understandable low-level language. In simple words, it converts the source code written
in a programming language into another form that the computer understands. Here in our case, we need one C++ compiler which will
convert our C++ source code into machine level codes. Based on the platform we are working on, there are different ways to set up the
compiler.
We can see these here one by one.- Windows:-
There are different IDEs available for Windows OS on which we can work easily with C++ programming language. One of the popular
IDE is Visual Studio Code by Microsoft. For this, there are a lot of plugins available which makes our work easy while coding. I have
covered its installation and configuration on my youtube channel. You can visit this link for the same. Also, there are many other IDE
available like Turbo C++, Code Blocks, etc. - Linux:-
For the Linux platform, we have to install the GNU GCC compiler. The installation and setup steps for the same as below:
-
First we need to install the GCC compiler. For this open the terminal window and execute the below two commands.
sudo apt-get update
sudo apt-get install GCC -
After that we need to install all the libraries which are required to compile and run a C++ program. Command to this is:
sudo apt-get install build-essential -
If the above two steps are completed without any error then just to check the GCC compiler is installed correctly, execute the below command:
g++ --version
This will print the current version of the GCC compiler installed.
-
First we need to install the GCC compiler. For this open the terminal window and execute the below two commands.
- Mac OS :-
In case of Mac OS the best way to set up the GCC is to download the Xcode development environment from Apple's AppStore. The link for downloading
Xcode is:- developer.apple.com/xcode/
Also all the necessary instruction to install the same are already available in that page.
- Windows:-
Sample Test program:
Now since our environment setup is completed, let's check with our first test code. For this first we have to open a text editor and add the
below code:
#include < iostream>
using namespace std;
int main() {
cout << "Hello Geek! Welcome to your first step to learning C++";
return 0;
}
Before compilation lets first look at a short breakdown of the above code:
- In the first line iostream is the header file or we can say cpp library file which we have to include in our program for input and output functions.
- using namespace std; provides information to the compiler to use the standard namespace. Namespace is the feature of C++ for scope handling.
- int main() is the main function from where the program execution starts.
- In the next line cout << "Hello Geeks! Welcome to your first step to learning C++"; is printing the message on the screen.
- After that in the next line return 0; indicate the end of the program. It will termiante the main function and return the value 0 to the calling process.
Compilation and Running:
For this, we first need to save the above text file where we have written the code with the format ".cpp", let say we save it as "test.cpp".
After that
below steps need to be followed.
- Open the terminal and the go to the path where "test.cpp" is saved.
- Then give the below command:
Here test.cpp is the our filename which we are targetting to compile. "g++" is the gcc command to compile the code. "-o" is the compilation option flag
which will direct the compiler to create the executable having name "test". -
On executing the above command, you observed that a new file with name "test" is automatically get created in the same directory where we have our
source file.
Now we have to run the program and for this give the below command next in the terminal:
This command will run our program and print the output message "Hello Geek! Welcome to your first step to learning C++" in the terminal window.