c++ - Sublime Text 2 运行 C++11 代码

标签 c++ c++11 sublimetext2

我在 Sublime Text 2 中有以下代码:

#include <iostream>
#include <string>

using std::cout;
using std::string;

int main()
{
    string s("some string");
    if (s.begin() != s.end())   // make sure s is not empty
    {
        auto it = s.begin();    // it denotes the first character in s
        *it = toupper(*it);     // make that character uppercase
    }

    cout << s;

    return 0;
}

当我将 C++.sublime-build 文件更改为

时,我可以通过按 ctrl-B 在 Sublime 中构建 C++11 代码
"cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],

但我想同时构建和运行代码,我通常使用ctrl-shift-B 来执行此操作。在编辑我的 sublime-build 文件之前,我从编译器收到一条错误消息:

/home/michael/src/c++-primer/pointers.cpp: In function ‘int main()’:
/home/michael/src/c++-primer/pointers.cpp:12:14: error: ‘it’ does not name a type
         auto it = s.begin();    // it denotes the first character in s
              ^
/home/michael/src/c++-primer/pointers.cpp:13:10: error: ‘it’ was not declared in this scope
         *it = toupper(*it);     // make that character uppercase

这向我暗示,当我按下 ctrl-shift-B 时,它并没有使用我编辑的 sublime-build 文件。我该如何更改?

最佳答案

哦,我想通了。您需要在构建文件的“变体”部分添加标志:

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

关于c++ - Sublime Text 2 运行 C++11 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25480152/

相关文章:

php - 是否可以在 Sublime Text 2 中自动格式化 PHP?

javascript - 在 Sublime Text 下获得完整的 JS 自动完成

c++ - 导致在 32 位代码中使用 64 位变量会导致性能下降

c++ - 继承时的作用域规则 - C++

sublimetext2 - Sublime Text 中基于缩进的颜色编码背景

c++ - Shared_ptr<X> 的原始数组,其中 X 似乎没有初始化?

c++ - C++11 中访问指向 std::vector 中元素 n 的指针的标准方法是什么?

c++ - 对 udp::socket::async_receive_from 的多个并行调用 - 未定义的行为?

c++ - 使用对象指针时如何访问成员函数

c++ - 迭代超过 10GB+ 的二进制文件