在Ubuntu中安装c++(安装则跳过)
参考:https://blog.csdn.net/W1995S/article/details/117876875
1. 查看gcc、g++是否安装
gcc --version
g++ --version
2.安装
sudo apt-get install gcc
sudo apt-get install g++
或指定版本:8
sudo apt-get install gcc-8
sudo apt-get install g++-8
报错1:
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavialable)
E: Unable to acquire the dpkg fronted lock (/var/lib/dpkg/lock-frontend), is another process using it?
解决:
sudo rm -rf /var/cache/apt/archives/lock-frontend
sudo rm -rf /var/lib/dpkg/lock-frontend
报错2:
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
解决:
sudo rm /var/lib/dpkg/lock
sudo dpkg --configure -a
3.创建cpp文件
创建项目目录cppdemo,用于学习创建第一个C++项目,并进入cppdemo目录中:
mkdir ~/data/yx/cpp/cppdemo && cd ~/data/yx/cpp/cppdemo
vi是打开文件,创建是vim
vi hello.cpp
#include <iostream>
int main()
{
printf("hello, World!\n");
system("pause");
return 0;
}
4. 运行
用g++编辑器编译,生成可执行文件 hello
g++ hello.cpp -o hello
注: -o 后面的hello是生成的存储输出内容的文件名
执行可执行文件(即打开hello)
./hello