Install g++
Linux compiles C++ program must install g++ compiler. Use yum to install here. First switch to the root account, su-root and enter the password.
Execute yum install gcc-c++ (note that it is not yum install g++), and an error is reported.
The error is reported because yum needs to configure the correct server address. The server provides the yum installation package, which is also called yum source. The configuration file for configuring the yum source is in the /etc/yum.repos.d/ directory, you can see that the system comes with two files.
cat file name, it will print all the contents of the file. You can see that the two files are either not configured, or the address is not accessible.
You can download the yum source address provided by major domestic manufacturers from the Internet. I downloaded the source address file CentOS6-Base-163.repo provided by 163 NetEase here.
Use ftp upload tool CentOS6-Base-163.repo when the newspaper made a mistake, because I landed ftp not using the root account, upload files and directories that only root has access to
use root landing ftp tool, successfully uploaded
to try now yum command
this time have returned the results, enter y, and press enter to automatically download and install. There will be an update prompt later, also enter y and press Enter, and the final message shows that the installation is successful.
Execute the g++ --version command to try
g++ installation successfully.
Compile and run C++ source code
ftp uploads the bubble sort code file create_bubblesort.cc to Linux, the code is as follows
#include<iostream>
#include<string>
using namespace std;
void BubbeSort(int arr[], int n)
{
int i, j, temp;
bool exchange;
for(i = 0; i < n; i++)
{
exchange = false;
for(j = n - 1; j >= i; j--)//前i个是最大的i个
{
if(arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
exchange = true;
}
}
if(!exchange)
return;
}
}
int main()
{
int arr[10] = {3,8,66,3456,4654,21,88,55,99,66};
BubbeSort(arr, 10);
for(int i = 0; i <10; i++)
cout<<arr[i]<<endl;
return 0;
}
Executing g++ create_bubblesort.cc will generate the executable file a.out. Execute a.out to input the sorting result.
gdb debugging
Linux debugging C++ code requires gdb. yum installation.
gdb debugging process following
last (gdb) environmental inputs quit, press enter, leave gdb return shell.
to sum up
After installing g++ gdb, the environment is set up. If the yum source configuration is correct, there will be no major problems.