实验室电脑没有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. |
配置方法
假设%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" 后面的"$" 为"$\MinGW\bin"(g++.exe等所在目录),其他的也可以根据需要自己修改,注意反斜杠要转义;
CTRL+S或点击菜单文件(File)->保存(Save)保存文件,命名为User-C++.sublime-build(或其它)。
大功告成。