c/c++ Development Environment Setup Using IDE

Introduction

c/c++ Development Environment Setup is required to write, compile, run the executable generated of a c/c++ program we need application software (IDE) and compilers as well.

In this tutorial you will be able to install Visual Studio Code IDE and required plugins and gcc and g++ compiler.

Before installing IDE and setting up development environment we first look at some basic terminologies.

IDE refers to the Integrated development environment. The compiler is a program that converts human-readable code into machine-understandable code. The process of converting human-readable code into machine code is called a compilation. Machine understandable code is binary.

You can make use of “Hello World! Program in C”.

Read more on Build Process which explains how human readable code is converted into machine code.


List of c/c++ compilers

  • GNU GCC compiler
  • GNU G++
  • Acorn C/C++
  • Ch
  • Cygwin
  • The SubC Compiler
  • Small-C
  • XL C, XL C/C++
  • Open64 (openCC)

Note: few compilers are only listed.


Development environment setup in Linux

If you are a linux user then follow steps below.

  1. Installing Visual Studio code in linux.
  2. Installing gcc and g++ in linux.
  3. Compiling c/c++ program in linux.
  4. Run the executable in linux.

Installing Visual Studio code in Linux.

You can simply install this IDE using command below:

sudo snap install --classic code 
sudo snap install --classic code 
[sudo] password for pranav: 
snap "code" is already installed, see 'snap help refresh'
Install visual studio code in ubuntu
Install visual studio code in ubuntu

I have already installed it so it is showing like already installed.

Open extensions and install C/C++ for Visual Studio Code extension.

Add-c-c-extensions-in-visual-studio-code

Open the folder in which the c/c++ program you want to keep by clicking on File->Open Folder [Ctrl+K+Ctrl+O].

Browse for the directory and create c/c++ file in that directory.

Open the file and write your program.


Installing gcc and g++

Run these commands in terminal.

Check if gcc is already installed by using command:

gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You can also check g++ version

g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If you get output as above then g++ is already installed and you can skip installing gcc step.

Below method will install both gcc and g++.

Update packages:

sudo apt update

Install build essentials package:

sudo apt install build-essential

If you want man pages then install it by command:

sudo apt-get install manpages-dev

Check for gcc version after installation

gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Check for g++ version after installation

g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compiling c/c++ program

We have already wrote c/c++ program .

Open a new terminal by clicking on top bar.

Open new terminal in visual studio code
Open new terminal in visual studio code

To compile c program run below command in terminal:

gcc -c filename.c

After compiling the c program you will find the object file in the directory.

To compile c++ program run below command in terminal:

g++ -c filename.cpp

After compiling the c/c++ program you will find the object file in the directory with extension .o.

Now you have to convert object file to convert use below command.

To convert a c object file:

gcc object_file.o

To convert a c++ object file:

g++ object_file.o

Now the executable file is being generated with name a.out.

You can rename the executable name while converting object file. as

For c object file

gcc object_file.o -o output

For c++ object file

g++ object_file.o -o output

Run the executable

Inorder to run the executable run the command: ./executable_name .

If the execuatble name is a.out.

./a.out

If the execuatble name is output.

./output

Development environment setup in windows

Steps to setup development environment setup in windows

  1. Installing Visual Studio code in windows.
  2. Installing gcc / g++ windows.
  3. Setup Environment variable.
  4. Compiling c/c++ program in windows.
  5. Run the executable.

Installing Visual Studio code in windows

Follow steps to Install Visual Studio code in windows

  1. Download VS Code.exe executable file and save it.

2. Run the executable as administrator.

3. Accept Licence agreement and click next.

Accept licence agreement in visual studio code
Accept licence agreement in visual studio code

4. Select installation folder and click next.

Select installation destination in visual studio code
Select installation destination in visual studio code

5. Select start menu folder and click next.

Select start menu folder in visual studio code installer
Select start menu folder in visual studio code installer

6. Select additional tasks and click next.

Select additional tasks options in visual studio installer
Select additional tasks options in visual studio installer

7. Review settings and click next.

Review settings in visual studio code installer
Review settings in visual studio code installer

8. Visual studio code installation is successful so check whether to lauch or not then click finish.

Visual studio code isntallation is successful
Visual studio code isntallation is successful

If you have checked the Launch visual studio check box then it opens automatically. If it is not opened then open Visual Studio code by searching in the start.

Install “C/C++ for Visual Studio Code extension” by searching it in extensions of Visual Studio Code.

C c++ plugin installation in windows visual studio code

Open the folder you want to keep the c/c++ source file by clicking on files on the top bar and choosing Open Folder or press [Ctrl+K+Ctrl+O].

Browse the folder and select it.

Then create a c/c++ program in that folder.


Installing gcc / g++ windows

  1. Download winbuild and save it.

2. Then run it with administrator permission by right clicking on win build.exe and choose run as administrator.

3. Select system installer type and architecture, then browse where you want to install the compiler.
I have installed it on C:\MinGW.

win build configuration window

4. Then click on ok.

5. Then choose the win build module choosing window and hit process button at top rightcorner.

win-build install module window

6. After successfull intsallation of all libraries. proceed to setting up environment variable for gcc and g++.


Setup Environment variable.

  1. Click on start menu and search “Environment variables” and click on edit environment variables of user.
Start menu search environment variables
Start menu search environment variables

2. When system properties is openered then click on Environment variables.

System Properties environment variable setting
System Properties

3. Click on new environment variable.

New user environment variable
New user environment variable

4. Add new environment variable with name and values as name is optional and value is installation folder/bin. Example: C:\MinGW\bin .

Then click ok.

Setup Environment variable value for gcc pic

5. After saving it find system environment variable "path" and click edit.

Setting new System Variable
Setting new System Variable

6. Click on new and set new path as installation folder/bin. Example: C:\MinGW\bin .

Then click ok and close this window.

Setting values for system variable minngw
Setting values for system variable minngw

7. Then open command prompt by searching in start menu and check for gcc version.

Command:

gcc --version
GCC Version check in windows

You will see a sample output like above.

If you see an error like “GCC is not recognized as an internal or external command” then check and correct the path values.

Command to check g++ version.

g++ --version

Compiling c/c++ program in windows.

Inorder to compile the c/c++ code write and save your code.

  1. Open new terminal by clicking on Terminal->New Terminal.
Open terminal in Visual studio code

2. Compile c program in windows run below command in terminal:

gcc -c filename.c

After compiling the c program you will find the object file in the directory.

To compile c++ program in windows run below command in terminal:

g++ -c filename.cpp

After compiling the c/c++ program you will find the object file in the directory with extension .o.

Now you have to convert object file to convert use below command.

To convert a c object file:

gcc object_file.o

To convert a c++ object file:

g++ object_file.o

Now the executable file is being generated with name a.out.

You can rename the executable name while converting object file. as

For c object file

gcc object_file.o -o output

For c++ object file

g++ object_file.o -o output


Run the executable.

Inorder to run the executable run the command: ./executable_name .

If the execuatble name is a.out.

./a.out

If the execuatble name is output.

./output

Sample output of hello scholarsoul program is as follows:

Sample c program in visual studio code

Conclusion

You can also use eclipse IDE, NetBeans IDE for the development of c/c++. Installation steps are a bit difficult hence in this tutorial I preferred Visual Studio Code as a development IDE.

Note: Software/IDE used in this tutorial is for educational purposes only. ScholarSoul.com neither owns those software nor claims it as its the property of ScholarSoul.com.