sublime 配置C++

时间:2023-03-09 04:51:33
sublime 配置C++

  1. 安装C语言编译器MinGW,并把MinGW安装目录下的bin目录添加到环境变量PATH里。详细方法参照MinGW安装和使用

2. 在SublimeText安装目录下的Data\Packages\User新建一个C.sublime-build(注意不要修改文件名和大小写)。因为SublimeText默认的执行不支持输入,所以我添加了一个RunInCommand,可以让程序在CMD下面执行。内容如下

{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++", "variants":
[
{
"name": "Run",
"cmd": ["cmd", "/c", "g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "cmd", "/c", "${file_path}/${file_base_name}"]
},
{
"name": "RunInCommand",
"cmd": ["cmd", "/c", "g++", "${file}", "-o", "${file_path}/${file_base_name}", "&&", "start", "cmd", "/c", "${file_path}/${file_base_name} & pause"]
}
]
}

3. 设置快捷键。在SublimeText里,打开Preferences -> Key Bindings - User,在里面添加一行{ "keys":
["ctrl+alt+shift+b"], "command": "build", "args": {"variant": "RunInCommand"} }。如果已经设置,跳过。我定义的是ctrl+alt+shift+b,也可以根据自己的定义,以下是我的 Key Bindings - User文件内容

[
{ "keys": ["ctrl+alt+shift+b"], "command": "build", "args": {"variant": "RunInCommand"} }
]

4. 测试。自己在SublimeText写一个C++程序,ctrl+b为编译;ctrl+shift+b为编译后在SublimeText里显示结果;ctrl+alt+shift+b为编译后在CMD里运行。