目錄
- WSL2安裝CentOS7
- 下載安裝包
- 安裝
- 啟動CentOS7
- CentOS7更換國內源
- gcc
- 從源碼安裝gcc
- 卸載gcc
- CMake中使用gcc
- 關于linux配置文件
- 參考
WSL2安裝CentOS7
Windows11官方WSL2已經支持Ubuntu、Open SUSE、Debian。但是沒有centos,所以centos的安裝方式略有不同。
下載安裝包
下載安裝包:https://github.com/mishamosher/CentOS-WSL?tab=readme-ov-file
安裝
找到一個空間大點的盤,解壓這個zip,然后雙擊CentOS7.exe
等待一會就安裝好了,在powershell中執行
wsl --list
就可以看到安裝好的子系統了,我自己裝了好幾個。
關于wsl的操作可以執行
wsl --help
查看更多參數
啟動CentOS7
在powershell中執行
wsl -d CentOS7
即可啟動CentOS7。
CentOS7更換國內源
比較常用的有阿里源,清華源等,這是阿里源的地址,其中有更換源的教程。
gcc
從源碼安裝gcc
進到centos里之后,首先從這里這里你需要的gcc版本,然后解壓。類似:
wget https://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz
tar xf gcc-9.2.0.tar.gz
如果找不到wget就用yum安裝
yum install wget
在下載的同時可以裝一下開發工具
yum groupinstall "Development tools" -y
編譯安裝gcc
cd gcc-9.2.0
# Configure the build, --disable-multilib支持64位,--enable-languages=c,c++僅編譯C和C++編譯器
./configure --disable-multilib --enable-languages=c,c++# Build and install
make -j8
sudo make install
卸載gcc
gcc沒有類似make uninstall的命令,只能手動刪除。但是文件比較多,可是使用腳本刪除。但是我們不知道要刪除哪些文件,可以重新將gcc安裝到一個指定臨時的路徑,之后根據臨時路徑中的文件,刪除默認安裝路徑(/usr/local/)的文件
rm -rf gcc-9.2.0 # 直接重新configure有些路徑還是默認的,所以直接刪了,重新解壓
tar xf gcc-9.2.0.tar.gz
cd gcc-9.2.0
./configure --disable-multilib --enable-languages=c,c++ --prefix=/home/work/gcc-deps/gcc-9.2.0_install
make -j8
sudo make install
cd /home/work/gcc-deps/gcc-9.2.0_install# [注意]防止誤操作,你可以先執行echo,確認沒有問題,再執行rm
find . -type f -print0 | while IFS= read -r -d '' file; do basename="$(basename -- "$file")"; find /usr/local/ -type f -name "$basename" -exec echo {} +; donefind . -type f -print0 | while IFS= read -r -d '' file; do basename="$(basename -- "$file")"; find /usr/local/ -type f -name "$basename" -exec rm {} +; done
CMake中使用gcc
安裝了gcc 9.2.0,但是cmake中找到的還是gcc 4.8.0,有兩種方式設置gcc
- 命令行參數
cmake -D CMAKE_C_COMPILER=/path/to/gcc/bin/gcc -D CMAKE_CXX_COMPILER=/path/to/gcc/bin/g++
- 修改CMakeLists.txt
SET(CMAKE_C_COMPILER "/home/hhb/gcc-9.2.0/bin/gcc")
SET(CMAKE_CXX_COMPILER "/home/hhb/gcc-9.2.0/bin/g++")
project(PROJECT_NAME) # 注意需要project之前
關于linux配置文件
linux下主要有四個配置文件:/etc/profile 、/etc/bashrc 、/root/.bashrc 、/root/.bash_profile。
- /etc/profile 設置的是系統全局環境和登錄系統的一些配置,該配置對所有用戶生效; ?
- /etc/bashrc 是shell全局自定義配置文件,主要用于自定義 shell,該配置對所有用戶的shell都生效;
- /root/.bashrc 用于單獨自定義root用戶的 bash,只對root用戶的bash生效,如果要使elk用戶生效,則需要配置/home/elk/.bashrc文件
- /root/.bash_profile 用于單獨自定義root用戶的系統環境,只對root用戶生效,如果要使elk用戶生效,則需要配置/home/elk/.bash_profile。
這四個配置文件的加載順序如下:
? /etc/profile -> /etc/bashrc -> /root/.bashrc -> /root/.bash_profile
參考
- https://blog.csdn.net/jiexijihe945/article/details/132067793
- https://www.cnblogs.com/renshengdezheli/p/14131943.html
- https://ioflood.com/blog/install-gcc-command-linux/#:~:text=In%20most%20Linux%20distributions%2C%20the,command%20sudo%20yum%20install%20gcc%20.
- https://stackoverflow.com/questions/19816275/no-acceptable-c-compiler-found-in-path-when-installing-python
- https://wangchujiang.com/linux-command/c/yum.html#:~:text=yum%E5%91%BD%E4%BB%A4%E6%98%AF%E5%9C%A8Fedora,%E5%9C%B0%E4%B8%80%E6%AC%A1%E6%AC%A1%E4%B8%8B%E8%BD%BD%E3%80%81%E5%AE%89%E8%A3%85%E3%80%82
- https://stackoverflow.com/questions/17275348/how-to-specify-new-gcc-path-for-cmake