Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 254
Ivan Akulov Avatar asked Jul 14 '12 15:07

Ivan Akulov


2 Answers

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}'"]
        }
    ]
}
like image 122
salek Avatar answered Nov 16 '22 09:11

salek


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

like image 43
Thorbear Avatar answered Nov 16 '22 08:11

Thorbear