Windows10 Linux開發環境搭建(WSL)
一、WSL初始化
從Windows引用商店搜搜Linux,下載Ubuntu(可選18.04或者20.04都可),下載完成後開始選單點選安裝,會要求初始化使用者名稱和密碼。
二、建立root使用者
後續操作需要root許可權,Ubuntu預設是沒有root使用者的,需要先手動建立
sudo passwd root
輸入密碼
三、更改阿里雲下載映象
這一步非常重要,可以極大提升使用者體驗,從阿里開源映象站複製對應下載源,然後按這個步驟操作:https://blog.csdn.net/Mingzhul/article/details/105302399
注意先備份再修改,養成良好操作習慣。
四、安裝vscode
隨著時代的進步,這一步的操作貌似有幾種方式,應該都可以吧
- 可以參考這個,編譯安裝VSCode:https://blog.csdn.net/reeeeein/article/details/104628415
- 或者直接通過 Remote-WSL 直接啟動:https://blog.csdn.net/weixin_43876113/article/details/105261577 ,不知道是不是我已經用第一步安裝了的原因,這樣的確是可行的
- 從官網下載VSCode Linux安裝包安裝,我覺得這種比較通用,步驟如下:
這一步需要首先在Windows上安裝好VSCode及 Remote-WSL外掛,現在有 C/C++ Extension Pack 外掛,可以裝一大堆C++開發的外掛,其中就有這個。 然後從
cp /mnt/d/code_1.62.0-1635954068_amd64.deb ./
注意:WSL和Windows通過 /mnt/win磁碟路徑
共享磁碟,然後安裝:
dpkg -i code_1.62.0-1635954068_amd64.deb
這樣就再wsl上安裝好了VScode,在其他Linux上都可以這麼操作,萬能的VSCode,然後切換到程式碼目錄,例如在 /home/testcode
目錄下,VScode可以執行命令啟動(也可直接在Windows中用Remote-WSL連線啟動,區別在於啟動後工作空間名字不一樣...):
cd /home/testcode
code .
這樣就會在Windows上的VScode中遠端開啟當前 testcode 目錄。
五、C++開發配置
最後就是一些構建配置,在工程目錄下的 .vscode 目錄下,一般來說這個目錄和需要的檔案會由VSCode自動生成,需要了解其含義,適當手動修改:
C++工程配置 c_cpp_properties.json,會在直接執行專案時用到:
{
"configurations": [
{
"name": "Linux",
"includePath": [ // 標頭檔案查詢路徑
"${workspaceFolder}/**",
"../include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
gdb除錯配置 launch.json ,會在F5執行除錯時用到:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", //啟動配置的下拉選單中顯示的名稱
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/testServer", // 將要進行除錯的程式的路徑
"args": [],
"stopAtEntry": false, // 設為true時程式將暫停在程式入口處
"cwd": "${workspaceFolder}/bin", // 除錯程式時的工作目錄
"environment": [],
"externalConsole": false, // 除錯時是否顯示控制檯視窗
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build-debug" // 除錯會話開始前執行的任務,一般為編譯程式
}
]
}
settings.json,這個不知道有什麼用,貌似與用到的庫有關:
{
"files.associations": {
"functional": "cpp",
"initializer_list": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"numeric": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cfenv": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"typeinfo": "cpp",
"memory": "cpp",
"forward_list": "cpp",
"string": "cpp"
},
"cmake.configureOnOpen": true
}
tasks.json ,構建任務配置,配置了一些 make 引數
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build-release",
"command": "make",
"args": [
"-j4",
"-C",
"${workspaceFolder}/build/"
],
"type": "shell",
"problemMatcher": [
"$gcc"
]
},
{
"label": "build-debug",
"command": "make",
"args": [
"-j4",
"-C",
"${workspaceFolder}/build/"
],
"type": "shell",
"problemMatcher": [
"$gcc"
]
},
{
"label": "clean",
"command": "make",
"args": [
"-C",
"${workspaceFolder}/build/",
"clean"
],
"type": "shell",
"problemMatcher": [
"$gcc"
]
}
]
}