【问题标题】:Can't build C++ program using Sublime Text 2无法使用 Sublime Text 2 构建 C++ 程序
【发布时间】:2012-07-14 15:45:48
【问题描述】:

我想你们中的许多人正在使用或曾经使用过 Sublime Text 2 编辑器。我有一个奇怪的错误:无法构建 C++ 程序。

我的C++.sublime-build

{
    "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
    "working_dir": "${file_path}",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "selector": "source.c, source.c++",

    "variants":
    [
        {
            "name": "Run",
            "cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        }
    ]
}

我发现当cmd 数组包含任何替换表达式(如${file}$file)时,构建不会启动。否则它会启动。

编译器无关紧要。当我尝试"cmd": ["notify-osd", "$file"] 时,它没有用;但使用"cmd": ["notify-osd", "sometexthere"] 就可以了。

手工编译是正确的。

我的程序:

#include <iostream>

int main() {
    std::cout << "Hello World";
}

我使用 Ubuntu 12.04,32 位。 Sublime 编辑器版本:2.0.1。

如果这不是我可以问这个问题的地方,请告诉我什么是正确的。

【问题讨论】:

    标签: c++ build sublimetext sublimetext2


    【解决方案1】:

    编辑你的 C++.sublime-build 文件....就像一个魅力。

    {
        "cmd": ["g++", "-Wall", "-Wextra", "-pedantic", "-std=c++11",   "${file}", "-o", "${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++ -Wall -Wextra -pedantic -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
            }
        ]
    }
    

    【讨论】:

    • 这是在 sublime 上打开 C++11 编译的方法(也是版本 3)。对于 12.04,使用“-std=c++0x”。
    【解决方案2】:

    我可以为这个问题提供一个临时解决方案:使用 makefile。 Sublime Text 2 可以运行 makefile 来为你编译 c++。

    通过在 Sublime 论坛 (http://www.sublimetext.com/forum/) 提问,您更有可能得到这个问题的更好答案。 即使在那里,他们也可能有兴趣知道它“如何”不起作用(即,如果按下“构建”时根本没有发生任何事情,您可能需要指定它)。

    【讨论】:

    • 这是提出此类问题的正确地方。 Sublime 是一款出色的编辑器,StOv 用户自然会对它感兴趣。 OP 在基本代码的简单编译方面存在问题,并且 makefile 的“解决方法”......不值得接受答案的标记。
    • @PMarecki 希望任何寻找这个问题的答案的人都会有智慧去检查接受的答案和投票最多的答案。
    • @PMarecki 我认为使用 makefiles 是一个非常合理的建议。鉴于 OP 接受了答案,他似乎也接受了。
    【解决方案3】:

    我花了几个小时在 Sublime Text 上进行 C++ 编译,但我仍然有一些小问题(例如 Sublime Text 显然无法在外部窗口/控制台中执行程序)。

    这是我的配置文件:

    {
    "cmd": ["C:\\MinGW\\bin\\mingw32-g++.exe", "-Wall", "-time", "$file", "-o", "$file_base_name"],
    
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    
    "working_dir": "${project_path:${folder}}",
    
    "selector": "source.c",
    
    "shell": true,
    
    "encoding": "latin1"
    
    }
    

    (如果编译器不起作用,请确保将编码更改为utf8)

    另外,将 MinGW 的 bin 文件夹添加到操作系统的 Path 变量中(在开始菜单中查找“环境变量”,然后查找 Path 变量)。

    【讨论】:

      猜你喜欢
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多