Sublime Text 2 Run C++11 Code

冷暖自知 提交于 2021-02-19 08:40:08

问题


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?


回答1:


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

}



来源:https://stackoverflow.com/questions/25480152/sublime-text-2-run-c11-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!