C++ Basics (1) Setting up the C++ development environment

C++ is a general-purpose programming language that is widely used today for competitive programming. It has imperative, object-oriented, and generic programming features. C++ can run on many platforms like Windows, Linux, Unix, Mac, etc.

C++ is a general-purpose programming language that is widely used today for competitive programming. It has imperative, object-oriented, and generic programming features. 
C++ runs on Windows, Linux, Unix, Mac, and many other platforms. Before we start programming in C++. We need to set up an environment on our local computer to successfully compile and run our C++ programs. If you don't want to set up a local environment, you can also use an online IDE to compile your program.

Use an online IDE : IDE stands for Integrated Development Environment. An IDE is a software application that provides computer programmers with the facility to develop software. There are many online IDEs available that you can use to easily compile and run programs without setting up a local development environment.

#include<iostream>
using namespace std;
main()
{
	cout << "Learning C++ at juejin";
}

Set up the local environment

To set up your own personal development environment on your local machine, you need to install two important pieces of software:

  1. Text Editor : A text editor is a type of program used to edit or write text. We will use a text editor to type our C++ program. The normal extension for text files is (.txt), but text files containing C++ programs 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 the C++ programming language. These extensions help the compiler recognize that a file contains a C++ program. 
    Before you can start programming in C++, you must install a text editor to write programs. \

  2. C++ Compiler : Once you have installed a text editor and typed and saved the program in a file with a ".CPP" extension, you will need a C++ compiler to compile the file. A compiler is a computer program that converts a high-level language into a low-level language that a machine can understand. In other words, we can say that it converts source code written in a programming language into another computer language that a computer can understand. In order to compile a C++ program, we need a C++ compiler, which converts source code written in C++ into machine code. Below are details on setting up the compiler on different platforms.

    • Linux installation : We will install the GNU GCC compiler on Linux. To install and use the GCC compiler on a Linux machine, follow these steps:

      • You must first run the following two commands from a Linux terminal window:
sudo apt-get update
sudo apt-get install gcc
sudo apt-get install g++
  • This command will install the GCC compiler on your system. You can also run the following command:
sudo apt-get install build-essential
  • This command will install all the libraries needed to compile and run a C++ program. \
  • After completing the above steps, you should check whether the GCC compiler is properly installed in your system. For this, you have to run the command given below from the Linux terminal:
g++ --version
  • If you have completed the above two steps without any errors, then your Linux environment is set up and can be used to compile C++ programs. In a further step, we will learn how to compile and run C++ programs on Linux using the GCC compiler. \
  • Write your program in a text file and save it with any filename and .CPP extension. We wrote a program to display "Hello World" and save it in a file named "helloworld.cpp" on the desktop. \
  • Now you have to open a Linux terminal and move to the directory where you saved the file. Then you have to run the following command to compile your file:
g++ filename.cpp -o any-name
  • filename.cpp is the name of the source code file. In our example, the name is "helloworld.cpp", any-name can be anything you choose. This name will be assigned to the executable created by the compiler after compilation. In our case, we choose any-name as "hello". 
    We run the above command as:
g++ helloworld.cpp -o hello
  • After executing the above command, you will see that a new file is automatically created in the same directory where you saved the source file with the name any -name of your choice . 
    Now to run your program you have to run the following command:
./hello
  • This command will run your program in a terminal window. \

  • Windows installation : There are a lot of IDEs available for Windows OS with which you can easily work with the C++ programming language. One of the popular IDEs is Code::Blocks . To download Code::Blocks, you can visit this link. After downloading the installation file of Code::Blocks from the given link, open it and follow the instructions to install it.

    • After successfully installing Code::Blocks, go to the File menu -> select New and create an empty file. \
    • Now write your C++ program in this empty file and save the file with ".cpp" extension. \
    • After saving the file with a '.cpp' extension, go to the Build menu and select the Build and Run option. \
  • Mac OS X Installation : If you are a Mac user, you must download Xcode. To download Xcode, you must visit the Apple website, or you can search for it in the Apple App Store.

    • After successfully installing Xcode, open the Xcode application. \
    • 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 select a C++ template, select the Application option under the OS X section on the left sidebar. Now select Command Line Tools from the available options and click on the Next button. \
    • Provide all the necessary details in the next window such as Organization Name, Product Name, etc. But make sure the language of choice is C++. After filling the details, click on the Next button to proceed to the further steps. \
    • Choose where to save the project. After this, select the main.cpp file from the directory listing on the left sidebar. \
    • Now open the main.cpp file and you will see that a pre-written c++ program or template is provided. You can change this program according to your requirements. To run your C++ program, you must go to the Product menu and select the Run option from the drop-down menu . \

Another very easy to use and now the most popular IDE is VSC (Visual Studio Code), available for Windows and Mac OS.

  • windows installation (VS Code): first install Visual Studio Code according to your windows . Open the downloaded file, click Run -> (accept the agreement) Next -> Next -> Next -> (check all options) -> Next -> Install -> Finish. 
    Now you will be able to see the Visual Studio Code icon on the desktop. Download MinGW from this link . Click "Continue" after installation. Check all packages (right click -> mark installed). Now, click Install (left corner) -> Apply Changes. (This may take some time) o Open This Computer -> C Drive -> MinGW -> Bin. (Copy this path)
         o Right click on "This PC" -> Properties -> Advanced System Settings -> Environment Variables -> (Select Path in System Variables) -> Edit -> New -> Paste the path in Here and sure. Go to Visual Studio Code, and install some useful extensions (from the right sidebar, the last icon (probably)) -
               1) C/C++
               2) Code Runner
         o Now, go to Settings -> Settings -> Search Terminal -> Go to end of this page -> check [ Code-runner: Run In Terminal ]
    Yay! You can go now. Open any folder, create new files and save them with the extension ".cpp". \
  • Mac OS Installation (VS Code): First, install Visual Studio Code for Mac OS using this link . Then we will install the compiler MinGW. To do this, we first need to install Homebrew. To install Homebrew, open a terminal (cmd + space). Write Terminal and press Enter. Copy the given command in cmd
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 some time.

o Now we will install the MinGW compiler on Mac OS. Paste the given command into the terminal and press Enter.

arch -x86_64 brew install MinGW-w64  

This is also a time-consuming process, so please be patient!

o Go to Visual Studio Code, and install some useful extensions (from the right sidebar, last icon (probably)) -
                       1) C/C++
                       2) Code Runner

o Now, go to Settings -> Settings -> Search Terminal -> Go to the end of this page -> Check

[code runner: run in terminal]

Yay! Now our C++ development environment is set up. Open any folder, create new files and save them with the extension ".cpp".

Guess you like

Origin blog.csdn.net/qq_53544522/article/details/126809958