Writing C++ under Ubuntu

Writing C++ under Ubuntu


Writing C++ on Ubuntu, this chapter mainly introduces how to use vi/vim to edit a C++ source file in Ubuntu terminal window . By writing the simplest example "Hello, World!". Lead everyone to learn how to edit and compile C++ under the Ubuntu terminal
. It is required here that you will be able to use vi/vim on Ubuntu, that is, you are required to have a little
foundation for getting started with Ubuntu.
If you don't have these foundations, you can copy the C++ code to Windows and use a lightweight C/C++ integrated development environment (IDE) like Dev-C++ to write and compile.

Introduction to C++

C++ (c plus plus) is a statically typed, compiled, general-purpose, case-sensitive, and irregular programming
language that supports procedural programming, object-oriented programming, and generic programming. C++ is considered as an intermediate language, which combines
the characteristics of high-level language and low-level language.
C++ was designed and developed by Bjarne Stroustrup in 1979 at Bell Laboratories in Murray Hill, New Jersey .
C++ further expanded and perfected the C language, originally named C with classes, and later renamed C++ in 1983 . C++ is a superset of C, and in fact, any legal C program is a legal C++ program.
As of 2020, C++17 was released in 2017, which is already the fifth C++ standard. We have also seen or heard of C++98,
such a C++ standard, that is, C++ released in 1998, so it is called C++98, which is the first standard of C++.

To learn C++, we need to understand the concepts, not delve into the technical details of the language. As long as we take the basic concepts of C++ in Chapter 2, it will be helpful to learn Qt or write C++

C++ environment settings

In order to write this tutorial, the author also reinstalled a Ubuntu18.04. Build the environment from scratch. We first configure
the server address of the software source as the address of Alibaba Cloud. In this way, we can obtain software sources from China, and the download speed will be faster.

If we want to write C++ programs in Ubuntu, we need a text editor and a C++ compiler that can write code. In the newly
installed Ubuntu environment, GCC for compiling C language is not installed, nor is G++ for compiling C++. Execute the following
instructions to install the environment for compiling C language and C++.

sudo apt-get install gcc g++ 
sudo apt-get install lsb-core lib32stdc++6 // 安装其他库 

After the installation is complete, you can use the following command to view the installed versions of gcc and g++.

g++ -v 
gcc -v 

insert image description here

Write a simple C++ program

Enter the following command in the terminal, first we create a C++ directory, and then use the cd command to enter the C++ directory.
Then create the 01_hello_world directory, enter the 01_hello_world directory, and then use the vi command to edit 01_hello_world.cpp.

mkdir C++ // 创建一个 C++目录。 
cd C++ // 进入创建的 C++目录。 
mkdir 01_hello_world // 创建一个 01_hello_world 目录 
cd 01_hello_world // 进入 01_hello_world 目录下。 
vi 01_hello_world.cpp // 编辑 cpp 文件,拷贝下文的内容 

insert image description here
Copy the following content to 01_hello_world.cpp.

1 #include <iostream> 

2 using namespace std; 

3 int main() 

4 { 

5 cout << "Hello, World!" << endl; 

6 return 0; 

7 } 

On line 1, the C++ language defines some header files that contain necessary or useful information in the program. In the above
program, the header file is included.

Line 2, using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively
new concept in C++. Among them, std is the standard namespace in C++, that is, it is written in the standard library, and we can
call it directly.

Line 3, int main() is the main function, and the program starts to execute from here.

Line 5, cout << "Hello World" << endl; will display the message "Hello World" on the screen and break the line. "<<"
is the operator and endl is the newline statement.

Line 6, return 0; terminates the main( ) function and returns to the calling process

Execute the following statement to compile and run this simple C++ program.
g++ 01_hello_world.cpp -o 01_hello_world // Compile with g++. Added after -o is the output object file.
./01_hello_world // Execute under the terminal, print "Hello, World!" and wrap.

insert image description here

We can expand how to output multiple lines. Can be added indefinitely as below. Among them, we found that after printing the first Hello, world!, the newline was also used because "\n" was used. Statements of C language can be used in C++, which is a superset of C language.

1 #include <iostream> 

2 using namespace std; 

3 int main() 
4 { 

5 cout << "Hello, world!\n" << "Hello, world!" << endl; 

6 return 0; 

7 } 

In line 5, we use the "<<" insertion operator (overloaded operator) to insert a sentence of "Hello, world!" to print, so that two lines of "Hello, world!" are printed on the terminal.

Guess you like

Origin blog.csdn.net/qq_42700289/article/details/129431550