簡介
D語言發展了十多年,一直不慍不火。現在 go 和 rust 都火起來了, D還是那個樣子,現有的第三方開發庫少,很多第三方庫年久失修,IDE插件也不是很好用,容易崩潰,總之存在了很多不完善的地方。不過D語言自身也有很多吸引人的特性,對於我本人來說,作為一個和c/c++一樣類型的語言,卻能寫出腳本語言的輕松,而且性能強勁,是很有吸引力的。時隔多年,我又忍不住下載了一下D語言最新安裝包,准備嘗試一下,先后嘗試了 vs+visual-d 和 vscode+code-d 兩種方案,感覺 vscode 這個方案要更加好用點。由於資料少,這里記錄一下,方便后來人。
正文
安裝插件
首先我們需要安裝一下D語言的插件,如下:
我們一般選擇第一個安裝即可
創建D語言工程
我們新建一個文件夾,然后使用cmd進入該文件夾,使用dub指令來初始化一個D語言的工程目錄,這里可以查看一下dub的幫助
$ dub init -h
USAGE: dub init [<directory> [<dependency>...]] [<options...>]
Initializes an empty package of the specified type in the given directory. By
default, the current working directory is used.
Command specific options
========================
-t --type=VALUE Set the type of project to generate. Available types:
minimal - simple "hello world" project (default)
vibe.d - minimal HTTP server based on vibe.d
deimos - skeleton for C header bindings
-f --format=VALUE Sets the format to use for the package description
file. Possible values:
json (default), sdl
-n --non-interactive Don't enter interactive mode.
我們可以看到其支持的工程類型,這里我們選擇默認,也就是 minimal
dub init
然后該文件夾下面就會生成工程目錄
配置vscode編譯選項
配置編譯選項是很簡單的事情,因為D語言的vscode插件已經存在了幾種默認的編譯配置,我們按下 ctrl+shift+p
調出運行框,輸入build,然后選擇 任務:配置默認生成任務
,可以看到:
然后我們選擇自己需要的選項,點擊右邊那個配置齒輪,會轉到 tasks.json 配置文件,最終我配置好的如下:
{
"version": "2.0.0",
"tasks": [
{
"type": "dub",
"run": false,
"compiler": "dmd",
"archType": "x86_64",
"buildType": "debug",
"configuration": "application",
"_generated": true,
"problemMatcher": [
"$dmd"
],
"group": "build",
"label": "build debug",
"detail": "dub build --compiler=dmd -a=x86_64 -b=debug -c=application"
},
{
"type": "dub",
"run": false,
"compiler": "dmd",
"archType": "x86_64",
"buildType": "release",
"configuration": "application",
"_generated": true,
"problemMatcher": [
"$dmd"
],
"group": "build",
"label": "build release",
"detail": "dub build --compiler=dmd -a=x86_64 -c=application"
}
]
}
注意:
如果使用x86的話一定要在dub后指定 x86_mscoff
這個編譯選項,而不是 x86
,不然是沒法使用vscode的c++插件進行調試的,因為它無法識別其調試信息
配置vscode調試選項
我們創建一個調試配置 lanuch.json ,內容如下:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "啟動",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
]
}
配置vscode調試斷點選項
為了能夠支持對D語言源碼文件下斷點的操作,我們需要修改 settings.json 配置文件,做如下設置:
{
"debug.allowBreakpointsEverywhere": true
}
然后一切就ok了!