【问题标题】:Can't build even a simple program in Sublime Text 3甚至无法在 Sublime Text 3 中构建一个简单的程序
【发布时间】:2018-11-05 11:05:04
【问题描述】:

我刚刚从 Python 切换到 C++ 来实现数据结构和算法。我发现 Sublime Text 3 非常强大。我安装了它,将我的 Mingw-64 编译器添加到路径中,还添加了一个“构建系统”。我希望构建和运行任何基本的 C++ 程序就足够了。但是当我跑步时

#include <iostream>
using namespace std;

int main() {
   int n;
   cin>>n;
   cout<<n<<endl;
   return 0;
}

我知道,构建部分或编译器设置存在一些问题。我尝试了不同的编译器,从代码块到独立的 Mingw-64 编译器,都没有成功。过去,我使用过代码块,它从来不需要我创建一个 exe 文件或引用它。如果我重新启动程序,它会显示权限被拒绝错误,我知道它为什么会发生。

这是错误:

系统找不到文件 G:\Programming\C++\second.exe。 [在 15.0 秒内完成,退出代码为 1]

[shell_cmd: g++ "G:\Programming\C++\second.cpp" -o "G:\Programming\C++/秒" && "G:\Programming\C++/秒"] [目录: G:\Programming\C++] [路径:C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C :\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program 文件 (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program 文件\英特尔\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\MATLAB\MATLAB Production 服务器\R2015a\runtime\win64;C:\Program Files\MATLAB\MATLAB Production 服务器\R2015a\bin;C:\Program Files\MATLAB\MATLAB Production 服务器\R2015a\polyspace\bin;C:\Python27\;C:\Python27\Lib\site-packages\PyQt4;C:\Program 文件\Git\cmd;C:\Program 文件 (x86)\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev0\mingw32\bin;C:\Users\80LM0141IH\Anaconda3;C:\Users\80LM0141IH\Anaconda3\Library\mingw-w64\ bin;C:\Users\80LM0141IH\Anaconda3\Library\usr\bin;C:\Users\80LM0141IH\Anaconda3\Library\bin;C:\Users\80LM0141IH\Anaconda3\Scripts;C:\Users\80LM0141IH\AppData\ Local\Programs\Python\Python36\Scripts\;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;G:\Microsoft VS 代码\bin;C:\Users\80LM0141IH\AppData\Local\GitHubDesktop\bin;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;";C:\Program 文件 (x86)\Graphviz2.38\bin";C:\Program Files (x86)\Graphviz2.34\bin;]

【问题讨论】:

  • 您构建second 并尝试运行second.exe。这是有原因的。
  • 请注意,如果您包含用于执行构建的 sublime-build,则更容易诊断 Sublime 构建问题。
  • 我该怎么办,不要调用second.exe。我按 ctrl+shift+b 执行 cpp 文件。
  • 使用手册并学习在 Sublime Text 中构建和运行设置。
  • 你是对的。然而,没有官方文件有这类东西的链接。我在网站上查看了这个docs.sublimetext.info/en/latest/file_processing/…。这里也没有提到任何有用的材料。

标签: c++ sublimetext3


【解决方案1】:

在我们开始之前,您需要确保您已安装 C++ 编译器并正确配置其路径。确保您可以在命令行中调用g++ 命令。

我从您的代码中看到您需要来自标准输入的输入。 Sublime Text 的控制台不能接受输入。所以这也许就是问题所在。您需要在终端中运行此程序

尝试使用以下设置替换您的构建系统:

{
    "shell_cmd": "g++ -std=c++11 -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.cpp, source.cc, source.cxx",

    "variants":
    [
        {
            "name": "Run in Terminal",

            "linux": {
                "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal 
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name}; bash'", // for xterm
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -hold -e ${file_path}/${file_base_name}", // for xterm
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && konsole --hold -e ${file_path}/./${file_base_name}", // for konsole

            },

            "windows":{
                "shell_cmd":   "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k  $file_base_name "
                // "shell_cmd":   "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start \"$file_base_name\" call $file_base_name"
            },

            "osx":{
                "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
            },

            "shell": true,
        },
    ]
}

Ctrl + Shift + B 并选择C++ - Run in Terminal。它将在您的 cmd 中编译并运行该程序。

我可以在我的环境中正确运行您的代码 sn-p。如果您遇到任何问题,请告诉我。

【讨论】:

  • 哦,看了这么多,终于成功了。先生,需要很多像您一样的程序员!谢谢。
  • 如果我的回答解决了您的问题,请考虑accepting it
  • 我在 ubuntu 上运行了这个,但这并没有弹出终端。有什么原因吗?
  • 您的系统上是否安装了xterm 或其他GUI 终端?您必须从适合您的不同选项中进行选择。
  • 哦,我修好了,当你说的时候,它对你有用。问题是我将程序保存在一个目录中,崇高文本无法访问该目录。它可以通过 ubuntu 和终端上的 GUI 访问,而不是通过 sublime 文本访问。现在它是固定的。我非常感谢您的关心。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
相关资源
最近更新 更多