Can't build C++ program using Sublime Text 2

2019-01-25 16:25发布

问题:

I think many of you are using or used to use Sublime Text 2 editor. I have strange error: C++ programs can't be built.

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

I found that when the cmd array contains ANY substituted expression like ${file} or $file, build doesn't start. Otherwise it starts.

It doesn't matter from compiler. When I've tried "cmd": ["notify-osd", "$file"], it didn't work; but with "cmd": ["notify-osd", "sometexthere"] it worked.

Compiling by-hand works right.

My program:

#include <iostream>

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

I use Ubuntu 12.04, 32bit. Version of Sublime Editor: 2.0.1.

If it isn't the place where I could ask this question, please tell me what's the right one.

回答1:

Edit your C++.sublime-build file....works like a charm.

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


回答2:

I can provide a workaround solution to this problem: Use makefiles. Sublime Text 2 can run makefiles to compile c++ for you.

You would be more likely to get a better answer to this question by asking in the Sublime forums (http://www.sublimetext.com/forum/). Even there they would probably be interested in knowing "how" it doesn't work (i.e. if nothing happens at all when pressing "Build", you might want to specify that).



回答3:

It took me several hours to get C++ compiling working on Sublime Text, and I still have some slight problems (like the fact that Sublime Text can't apparently execute the program in an externe window/console).

Here's my config file:

{
"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"

}

(make sure to change the encoding to utf8 if the compiler doesn't work)

Also, add the MinGW's bin folder to your OS's Path variable (look up 'environment variable' in the start menu, and then look for the Path variable).