【问题标题】:Sublime text gives error on creating a build system for c++ 14Sublime text 在为 c++ 14 创建构建系统时出错
【发布时间】:2021-01-15 09:07:12
【问题描述】:

我在我的 Sublime Text 3 for C++ 14 中创建了一个新的构建系统,并粘贴了以下代码并将其保存为 c++ 14。

{
 "cmd":["bash", "-c", "g++ -std=c++14 -Wall '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"],
 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 "working_dir": "${file_path}",
 "selector": "source.c, source.c++",
 "variants":
 [
   {
     "name": "Run",
     "cmd":["bash", "-c", "g++ -std=c++14 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
   }
 ]
}

但是每当我选择这个构建(保存 C++ 14)来运行我的 c++ 程序时,我的 Sublime Text 就会给出以下错误:

[WinError 2] The system cannot find the file specified
[cmd: ['bash', '-c', "g++ -std=c++14 -Wall 'C:\\Users\\ragha\\Desktop\\All_Folders\\sublime_cpp\\_code.cpp' -o 'C:\\Users\\ragha\\Desktop\\All_Folders\\sublime_cpp/_code' && 'C:\\Users\\ragha\\Desktop\\All_Folders\\sublime_cpp/_code'"]]
[dir: C:\Users\ragha\Desktop\All_Folders\sublime_cpp]
[path: C:\Python38\Scripts\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Calibre2\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft VS Code\bin;C:\Program Files\Java\jdk-14.0.1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Python38\python.exe;C:\Python38;C:\MinGW\bin;C:\Users\ragha\AppData\Local\Microsoft\WindowsApps;C:\src\flutter\bin;C:\MinGW\bin;]
[Finished]

注意 - 我已经安装了 MinGW C++ 安装程序。我已将 C:/MinGw/bin 添加到 Global Variables as well as System Variables 的路径中。当我使用C++ single file build 运行我的 C++ 程序时,它运行良好。我的系统中没有任何 WSL。

如何解决问题。

请帮忙!

【问题讨论】:

  • 如果你只使用MinGW而不使用WSL,那么你可能没有bash,你也不需要它。内置构建和您的构建之间的区别主要在于。只需复制C++ Single File.sublime-build的内容并修改参数即可,不需要bash

标签: c++ windows sublimetext3 mingw


【解决方案1】:

这是一个可以尝试的新构建系统:

{
    "shell_cmd": "bash -c \"g++ -std=c++14 -Wall '${file}' -o '${file_path}/${file_base_name}.exe' && '${file_path}/${file_base_name}'\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "variants": [
    {
        "name": "Run",
        "cmd": ["bash", "-c", "'${file_path}/${file_base_name}'"]
    }]
}

"shell_cmd" 将其所有参数作为一个字符串,因此我根据需要将它们与转义的双引号组合在一起,并进行了一些调整以使其运行,至少对我而言。

我还将您的Run 部分更改为只运行已编译的二进制文件。在您原来的版本中,您在运行之前再次编译。你会注意到我在这里使用了"cmd" 函数,其中的参数是一个逗号分隔的字符串数组。

请注意,这个构建系统仍然在 Sublime 的控制台中运行二进制文件,所以你不能做一些事情,比如请求用户输入。但是,如果您想在单独的命令提示符下运行程序以接受用户输入,例如,请使用以下构建系统:

{
    "shell_cmd": "g++ -std=c++14 -Wall ${file} -o ${file_path}/${file_base_name}.exe && start cmd /k ${file_path}/${file_base_name}",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "shell": true,

    "variants": [
    {
        "name": "Run",
        "cmd": ["start", "cmd", "/k", "$file_base_name"],
        "working_dir": "${file_path}",
        "shell": true,

    }]
}

编辑

如果你只想编译和运行你的程序inside Sublime,这应该可以工作:

{
    "shell_cmd": "g++ -std=c++14 -Wall ${file} -o ${file_path}/${file_base_name}.exe && ${file_path}/${file_base_name}",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "shell": true,

    "variants": [
    {
        "name": "Run",
        "cmd": ["$file_base_name"],
        "working_dir": "${file_path}",
        "shell": true,

    }]
}

【讨论】:

  • 我尝试了第一个。出现错误:'bash' 未被识别为内部或外部命令、可运行程序或批处理文件。 [在 0.1 秒内完成,退出代码为 1] [shell_cmd: bash -c "g++ -std=c++14 -Wall 'C:\Users\ragha\Desktop\All_Folders\sublime_cpp_code.cpp' -o 'C:\Users \ragha\Desktop\All_Folders\sublime_cpp/_code.exe' && 'C:\Users\ragha\Desktop\All_Folders\sublime_cpp/_code'"] [目录:C:\Users\ragha\Desktop\All_Folders\sublime_cpp]跨度>
  • 使用第二个,我的 cmd 打开。我不想使用 cmd,因为我正在从输入文件 input.txt 获取输入,并将输出显示到 output.txt。我的 code.cpp、input.txt 和 output.txt 这三个都在同一个目录中。
  • @MattDMo 编辑工作。谢谢。现在我的程序运行良好。
猜你喜欢
  • 2020-10-07
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多