SublimeText下C编译环境的设置

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

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

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

Python
{
    "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文件内容

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

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

posted @ 2022-02-10 18:58  燕鹏  阅读(14)  评论(0)    收藏  举报  来源