0
点赞
收藏
分享

微信扫一扫

Sublime Text编译运行C++语言程序出现无法输入解决办法(Windows、Linux、MacOS均适用)

柠檬的那个酸_2333 2022-01-08 阅读 19

文章目录


项目场景

最近把电脑上的 SublimeText 编辑器更新到最新版了,在编译处理 C 语言程序时,发现只要是包含 scanfcin 等需要我们手动输入的程序,编译是没问题的,但是在 SublimeText 里运行会出现无法从命令行输入的情况,在外部运行可执行程序是没问题的。


问题描述

在使用 SublimeText 编译运行这段代码,我们可以看到无论在命令行中如何输入都读取不进去:

在这里插入图片描述

但是我在外部终端中直接运行编译生成的可执行文件 test,是可以运行的,说明是 SublimeText 那边的问题:

在这里插入图片描述


解决方案

自己重新创建一个C++的编译器:打开SublimeText → Tools → Build System → New Build System

考虑到篇幅问题C语言的我放在另一篇博客了。

Windows系统

Windows系统写入如下内容到文件内:

{
    "windows": {
        "cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_base_name}.exe"],
    },
    "cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_base_name}"],
    "file_regex": "^(.*)\\(([0-9]+),([0-9]+)\\) (Error|Fatal): (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",
    "variants": [
    {
        "name": "Run",
        "shell": true,
        "windows": {
            "shell_cmd" : "start cmd /c \"\"${file_base_name}.exe\" & echo. & pause\""
        }
    },
    {
        "name": "Build and Run",
        "shell": true,
        "windows": {
            "shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_base_name}.exe\" && start cmd /c \"\"${file_base_name}.exe\" & echo. & pause\""
        },
    }
    ]
}

MacOS系统

MacOS系统写入如下内容:

{
    "cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_base_name}"],
    "file_regex": "^(.*)\\(([0-9]+),([0-9]+)\\) (Error|Fatal): (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",
    "osx": {
        "path": "/usr/local/bin:/usr/bin:/bin:${path}",
        "cmd": ["clang++", "-std=c++11", "${file}", "-o", "${file_base_name}"],
    },
    "variants": [
    {
        "name": "Run",
        "shell": true,
        "osx": {
            "shell_cmd": "echo 'cd \"${file_path}/\"' > '/tmp/${file_base_name}' && echo './\"${file_base_name}\"' >> '/tmp/${file_base_name}' && echo read >> '/tmp/${file_base_name}' && chmod +x '/tmp/${file_base_name}' && open -a Terminal.app '/tmp/${file_base_name}'"
        },
        }
    },
    {
        "name": "Build and Run",
        "shell": true,
        "osx": {
            "shell_cmd": "clang++ -std=c++11 '${file}' -o '${file_base_name}' && echo 'cd \"${file_path}/\"' > '/tmp/${file_base_name}' && echo './\"${file_base_name}\"' >> '/tmp/${file_base_name}' && echo read >> '/tmp/${file_base_name}' && chmod +x '/tmp/${file_base_name}' && open -a Terminal.app '/tmp/${file_base_name}'"
        },
    }
    ]
}

Linux系统

Linux系统写入如下内容:

{
    "cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_base_name}"],
    "file_regex": "^(.*)\\(([0-9]+),([0-9]+)\\) (Error|Fatal): (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",
    "variants": [
    {
        "name": "Run",
        "shell": true,
        "linux": {
            "cmd": ["gnome-terminal -- bash -c \"\\\"./${file_base_name}\\\";echo ;read line;exit; exec bash\""]
        },
    },
    {
        "name": "Build and Run",
        "shell": true,
        "linux": {
            "cmd": ["g++ -std=c++11 \"${file}\" -o \"${file_base_name}\" && gnome-terminal -- bash -c \"\\\"./${file_base_name}\\\";echo ; read line; exit; exec bash\""]
        },
    }
    ]
}

保存文件到其给定位置(不要自己变更位置),命名为 MyC++.sublime-build


运行测试

接着我们在编译前,Tools → Build System → 勾选MyC++,使用 Ctrl+B 选择 Build and Run 便可以正常出结果了。

举报

相关推荐

0 条评论