简体   繁体   中英

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

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.

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

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 (ie if nothing happens at all when pressing "Build", you might want to specify that).

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).

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