简体   繁体   中英

Sublime Text 2 Run C++11 Code

I have the following code in 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;
}

I can build C++11 code in Sublime by pressing ctrl-B as I changed my C++.sublime-build file to

"cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],

But I want to build and run the code at the same and I usually do this with ctrl-shift-B . I get an error msg from the compiler which I received before editing my sublime-build file:

/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

This suggests to me that when I press ctrl-shift-B , it is not using the sublime-build file I edited. How can I change this?

Oh I figured it out. You need to add the flag in the "Variants" section of the build file:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM