Create a gist now

Instantly share code, notes, and snippets.

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

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
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
Owner
PhilCK commented Mar 17, 2012

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

@adnansky

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

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

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

@adnansky

Hi Adrian! It should be.

@feryardiant

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

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

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

@verydragon

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
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"]
        }
    ]
}
@minhhuy150894

Brilliant 😄

@sean-wang-wenfeng

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment