sublime text3如何在命令行运行C++程序

时间:2022-09-26 17:47:32

找了好久,在百度贴吧看到了一个比较好的解决方案,首先MinGW的环境要搭建好,环境变量要配置好

1、C_INCLUDEDE_PATH:E:\MinGW\include

2、PATH:E:\MinGW\bin

3、LIBRARY_PATH:E:\MinGW\lib

接着Tools->Build System->New Build System,复制下面代码

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

文件存为CPP.sublime-build,我感觉其他名字也可以

加上快捷键:Preferences->Key Binding User,加入以下代码:

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

tools->build system->刚才设置名字的system

ctrl+b编译,ctrl+shift+b命令行运行,代码就不解释了,建议你可以比较一下sublime text 2里面browse package->C++里面C++.sublime-build文件,学过Java的应该很容易理解

有时候可能软件出了问题,卸载以后重装发现还是老样子,可以尝试删除这个目录下的文件C:\Users\用户名\AppData\Roaming\Sublime Text 3