用Sublime编译c、c++的方法

来源:互联网 发布:进口化妆品数据分析 编辑:程序博客网 时间:2024/06/08 17:19

1、利用MinGw安装编译器并设置环境变量

2、为SublimeText原本的编译环境不支持非英语系统,所以要先修改SublimeText。把SublimeText安装目录下的Data\Packages\Default\exec.py的45行改成下面的代码:

   # proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())            proc_env[k] = os.path.expandvars(v.decode(sys.getfilesystemencoding())).encode(sys.getfilesystemencoding())

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

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

4、设置快捷键。在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"} }]

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



0 0
原创粉丝点击