Sublime Text 2运行C ++ 11代码

时间:2014-08-25 06:31:14

标签: 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;
}

我可以通过按ctrl-B 构建 C ++ 11代码,因为我将C++.sublime-build文件更改为

"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文件。我怎么能改变这个?

1 个答案:

答案 0 :(得分:1)

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

{
    "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}'"]
        }
    ]

}