WSL中配置C/C++开发环境
很多时候,我们都需要一个 Linux 环境,但是装虚拟机又比较麻烦,自从微软为 Win10内置了 WSL(Linux 子系统)后,我们一般的需求可以代替实体装 Linux 或者虚拟机,再也不用折腾装系统复杂的切换了,而且 WSL 与 VSCode 配合写代码也是一绝,这篇文章将介绍在新安装的 WSL 中搭建基本的 C/C++ 开发环境常用的软件。更改国内源参考:https://www.cnblog
很多时候,我们都需要一个 Linux 环境,但是装虚拟机又比较麻烦,自从微软为 Win10 内置了 WSL(Linux 子系统)后,我们一般的需求可以直接使用 WSL 代替实体装 Linux 或者虚拟机装 Linux,再也不用折腾装系统复杂的切换了,而且 WSL 与 VSCode 配合写代码也是一绝,这篇文章将介绍在新安装的 WSL 中搭建基本的 C/C++ 开发环境常用的软件。
更改国内源
参考:https://www.cnblogs.com/zqifa/p/12910989.html
第一步:备份源文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
第二步:编辑/etc/apt/sources.list文件
在文件最前面添加以下条目(操作前请做好相应备份):
vi /etc/apt/sources.list
网易163源
#默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
#预发布软件源,不建议启用
#deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
#deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
第三步:执行更新命令:
sudo apt-get update
sudo apt-get upgrade
常用国内源:
腾讯云源
deb http://mirrors.tencentyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tencentyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.tencentyun.com/ubuntu/ focal-updates main restricted universe multiverse
# deb http://mirrors.tencentyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb http://mirrors.tencentyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.tencentyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.tencentyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.tencentyun.com/ubuntu/ focal-updates main restricted universe multiverse
# deb-src http://mirrors.tencentyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src http://mirrors.tencentyun.com/ubuntu/ focal-backports main restricted universe multiverse
阿里云源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
清华源
#默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
#预发布软件源,不建议启用
#deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
网易163源
deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
安装neofetch
sudo apt install neofetch
安装/升级gcc/g++
参考:https://blog.csdn.net/xiangxianghehe/article/details/79675468
为了使用C++14/17/20的新特性,我们难免要升级下自己的GCC版本,同时还要保证自己新安装的GCC生效,并且和原GCC共存。
安装GCC-5.0
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5 g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5
其中最后一句会把当前的默认GCC版本设置为gcc-5。
安装GCC-7.0
sudo add-apt-repository ppa:jonathonf/gcc-7.1
sudo apt-get update
sudo apt-get install gcc-7 g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7
其中最后一句会把当前的默认GCC版本设置为gcc-7。
安装GCC-9.0
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9
其中最后一句会把当前的默认GCC版本设置为gcc-9。
或者试下下面命令:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:jonathonf/gcc-9.0
sudo apt-get update
sudo apt-get install gcc-9
安装GCC-10.0
sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa
sudo apt update
sudo apt install gcc-10 g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --slave /usr/bin/g++ g++ /usr/bin/g++-10
安装gdb
源里添加ubuntu20的源,可以直接安装最新版 gdb9.2。
sudo apt-get install gdb
仓库软件包查找
参考:https://www.jianshu.com/p/dde90215390e
查找软件包
apt-cache search 软件包名
显示软件包的详细信息
apt-cache show 软件包名
安装python
参考:https://blog.csdn.net/weixin_42311427/article/details/113546996
以root用户或具有sudo访问权限的用户身份运行以下命令,以更新软件包列表并安装必备组件:
sudo apt update
sudo apt install software-properties-common
将Deadsnakes PPA添加到系统的来源列表中:
sudo add-apt-repository ppa:deadsnakes/ppa
启用存储库后,请使用以下命令安装Python 3.8:
sudo apt install python3.8
通过键入以下命令验证安装是否成功:
python3.8 --version
指定python3版本
参考:https://zhuanlan.zhihu.com/p/67255387
ubuntu 系统自带 python2.7 和 python3.5 两个版本的 python,
一般情况下在终端输入python,默认打开的是python2.7,如何指定默认打开python3.5,操作如下:
sudo cp /usr/bin/python /usr/bin/python_bak
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.5 /usr/bin/python
以上完成设置python3.5为默认python,即输入python就返回的是python3.5的信息。
但输入python3时遇到如下问题:
$ python3
bash: /usr/lib/command-not-found: /usr/bin/python3: 解释器错误: 没有那个文件或目录
解决办法:
sudo apt-get install --reinstall python2.7-minimal 3.5
如果是一个破碎的系统链接,执行如下:
sudo ln -s /usr/bin/python3.5 /usr/bin/python3
或类似的位置找到你的python 3 二进制文件并链接它。
以上解决完成python3链接问题,即现在在终端输入python3和输入python都返回的是python3.5的信息。
安装protobuf
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive # 可能会失败,最好网络代理
./autogen.sh
./configure
make -j8
make check
sudo make install
sudo ldconfig # refresh shared library cache.
然后查看版本:
$ protoc --version
libprotoc 3.17.2
安装CMake
前提必须安装OpenSSL
git clone https://github.com/openssl/openssl.git
cd openssl
./config # --prefix=/usr/local --openssldir=/usr/local/openssl
make -j8
sudo make install
安装CMake
git clone https://github.com/Kitware/CMake.git
cd CMake
./bootstrap
make -j8
sudo make install
cmake --version
更多推荐
所有评论(0)