Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting Sublime Text to build a C++ file.

C++ Sublime Text Build

This builds single C++ files in Sublime Text, handy for testing small things.

Works With

  • OSX (Assuming xcode installed)
  • Linux (Should work if g++ is installed)
  • Windows (Need CL working)

Thanks to adnansky for Linux Test and Shell update.

{
"cmd": ["g++", "$file", "-o", "$file_base_name", "-I/usr/local/include"],
"selector": "source.c++",
"windows":
{
"cmd": ["cl", "/Fo${file_path}", "/O2", "$file"]
}
}
{
"cmd": ["g++ -Wall ${file} -o ${file_base_name} && ./${file_base_name}"], "working_dir": "${file_path}",
"selector": "source.c++",
"shell": true,
"windows":
{
"cmd": ["cl", "/Fo${file_path}", "/O2", "$file"],
"selector": "source.c++",
"shell": true
}
}
@adnansky

This comment has been minimized.

adnansky commented Mar 17, 2012

Nice! Do you know why 'g++/gcc' gives an 'error: &&: No such file or directory' with the line
"cmd": ["g++", "$file", "-o", "$file_base_name", "-I/usr/local/include", "&&", "./$file_base_name"],
Under windows it works with "shell:true" option. How can I redirect the output?
Thanks!

@PhilCK

This comment has been minimized.

Owner

PhilCK commented Mar 17, 2012

Hmmm, I'm not sure, I've not tested this in the newer versions of Sublime so it maybe that somethings changed.

tbh I've had mixed results with this, I need to give it another look, I've also switched to G++ under Windows for this kind of simple one files stuff.

@PhilCK

This comment has been minimized.

Owner

PhilCK commented Mar 17, 2012

Looking at the string and error you posted I don't know where the "&&" is coming from.

@adnansky

This comment has been minimized.

adnansky commented Mar 17, 2012

Your examples are correct. After buildind ST2's console shows only [Finished].
I just want to see the result or 'printf/puts' in the ST2's console like I do under Windows.
With minGW for example I use this sublime-build file:
{
"cmd": ["mingw32-g++", "$file", "-Wall", "-o", "$file_base_name", "&&", "$file_base_name"],
"file_regex": "^[ ]File "(...?)", line ([0-9]*)",
"working_dir": "${project_path:${folder}}",
"selector": "source.c",
"shell": true
}

And with g++/gcc under Unix-like OS we type in the console "g++ -Wall test.c -o test -I/usr/local/include && ./test" to build and run. But with "&&" it's not compiling, g++ gives an error "&&:No such file or dir".
I can't use "shell": true option as well, it gets worse.
Do you have any idea how can we make it work?
Is it possible to put a second command in this 'sublime-build' file to run the executable?
Thanks PhilCK!

@adnansky

This comment has been minimized.

adnansky commented Mar 18, 2012

I've made it Phil!
To build and run with ST2 under Linux:

{
"cmd": ["g++ -Wall ${file} -o ${file_base_name} && ./${file_base_name}"],
"working_dir": "${file_path}",
"shell": true
}

Thanks for your quick responds!
Nice coding!

@PhilCK

This comment has been minimized.

Owner

PhilCK commented Mar 18, 2012

Cool, thanks for that.
I've updated the Gist to include it. The console out makes a lot of sense.

@adrianmaurer

This comment has been minimized.

adrianmaurer commented Jun 20, 2012

thanks. by changing the compiler to gcc this should work for c code as well?

@adnansky

This comment has been minimized.

adnansky commented Jun 22, 2012

Hi Adrian! It should be.

@feryardiant

This comment has been minimized.

feryardiant commented Aug 29, 2012

it's should working on Linux distro with GTK+ 3

{
    "cmd": ["gcc -o ${file_base_name} ${file} `pkg-config --libs --cflags gtk+-3.0` && ./${file_base_name}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "shell":true
}
@Namco2k12

This comment has been minimized.

Namco2k12 commented Oct 2, 2012

Because Sublime Console does not seem to support cin commands, i run my compiled files in an external console.
I'm new to Linux, Sublime and C++, so i don't know if this is a good solution, but it works fine for me (Kubuntu 12.04):

{
    "cmd": ["g++ ${file} -o ${file_base_name}"], 
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",
    "shell": true,
    "variants":
    [
        {
            "name": "Run",
            "cmd": ["konsole --hold --nofork -e ${file_path}/./${file_base_name}"],
            "shell": true
        }
    ]
}  

The --hold option is used to enforce the terminal stays open after the programm finished and --nofork causes the terminal window to run in the front, so you don't have to switch between windows manually.

@nguyenvanquan7826

This comment has been minimized.

nguyenvanquan7826 commented Aug 20, 2013

Hi, I run my file *.cpp but when I use scanf(), I can't enter the number.
Can you help me?

@verydragon

This comment has been minimized.

verydragon commented Sep 25, 2013

I created an additional Cpp.sublime-build, put in your code, and relaunched sublime text. The build and run works like a charm.

Thank you!

@consty

This comment has been minimized.

consty commented Nov 14, 2013

Hello, here is what worked for me on MinGW.

{
    "cmd": ["g++", "$file", "-o", "$file_base_name"],
    "selector": "source.c++",
    "working_dir": "$file_path",

    "variants":
    [
        {
            "name": "Run",
            "cmd": ["g++", "$file", "-o", "$file_base_name", "&&", "$file_path/$file_base_name"],
            "shell": true
        }
    ]
}

Also another alternative is if you want to do Ctrl-B and Ctrl-Shift-B (twice the effort) you can do it like this:

{
    "cmd": ["g++", "$file", "-o", "$file_base_name"],
    "selector": "source.c++",
    "working_dir": "$file_path",

    "variants":
    [
        {
            "name": "Run",
            "cmd": ["$file_path/$file_base_name"]
        }
    ]
}
@justingtv

This comment has been minimized.

justingtv commented Jul 15, 2015

Brilliant 😄

@sean-wang-wenfeng

This comment has been minimized.

sean-wang-wenfeng commented Nov 1, 2015

I noticed that this is the solution for ST2. Any solution for ST3 to solve the external include path? Thanks.

@alex4men

This comment has been minimized.

alex4men commented Jan 30, 2018

@sean-wang-wenfeng, or anyone who is also interested in solution. This is how I made a sublime-build file for ST3 to build single C++ file with inclusion of openCV library:

{
    "cmd": ["g++ -Wall ${file} -o ${file_base_name} `pkg-config --cflags --libs opencv` && echo 'Build Finished'  && ./${file_base_name}"], 
    "working_dir": "${file_path}",
    "selector": "source.c++",
    "shell": true,

    "windows":
    {
        "cmd": ["cl", "/Fo${file_path}", "/O2", "$file"],
        "selector": "source.c++",
        "shell": true
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment