跳到主要內容

Sublime Text——便攜版編譯環境配置

· 閱讀需 3 分鍾

實驗室電腦沒有GCC,於是在U盤裡復制了一份MinGW的C++編譯環境。由於懶得每次配置環境變量,就想把MinGW放到Sublime目錄裡,直接使用。

但sublime的編譯系統默認的工作目錄是目標文件所在的目錄,所以編譯時提示找不到g++命令。由於U盤的盤符經常會改變,所以也無法固定設置。找了很久,終於找到了辦法。

sublime定義了一組特殊變量:

$file_path
The directory of the current file, e.g. C:\Files.
$file
The full path to the current file, e.g.C:\Files\Chapter1.txt.
$file_name
The name portion of the current file, e.g.Chapter1.txt.
$file_extension
The extension portion of the current file, e.g.txt.
$file_base_name
The name-only portion of the current file, e.g.Document.
$folder
The path to the first folder opened in the current project.
$project
The full path to the current project file.
$project_path
The directory of the current project file.
$project_name
The name portion of the current project file.
$project_extension
The extension portion of the current project file.
$project_base_name
The name-only portion of the current project file.
$packages
The full path to thePackagesfolder.
其中,$packages是可以定位到sublime安裝目錄中的 "Data\Packages" 目錄的。

配置方法

假設%DIR%是sublime安裝目錄;

復制MinGW目錄到%DIR%\Data\Packages目錄;

找到%DIR%\Packages\C++.sublime-package文件(注意和上面的Packages目錄不是同一個),復制一份到其它目錄,把文件後綴改為zip,解壓後找到C++ Single File.sublime-build文件,用記事本打開它,復制內容;

選擇sublime菜單->工具(Tools)->編譯系統(Build System)->新編譯系統(New Build System),新建一個編譯系統,把剛剛復制的內容粘貼到新建的文件中;

修改"working_dir" 後面的"${file_path}" 為"${packages}\MinGW\bin"(g++.exe等所在目錄),其他的也可以根據需要自己修改,注意反斜槓要轉義;

CTRL+S或點擊菜單文件(File)->保存(Save)保存文件,命名為User-C++.sublime-build(或其它)。

大功告成。