Create a gist now

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.

Show comment
Hide comment
@adnansky

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

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.

Show comment
Hide comment
@PhilCK

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

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.

Show comment
Hide comment
@PhilCK

PhilCK Mar 17, 2012

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

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.

Show comment
Hide comment
@adnansky

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

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.

Show comment
Hide comment
@adnansky

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

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.

Show comment
Hide comment
@PhilCK

PhilCK Mar 18, 2012

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

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.

Show comment
Hide comment
@adrianmaurer

adrianmaurer Jun 20, 2012

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

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

@adnansky

This comment has been minimized.

Show comment
Hide comment
@adnansky

adnansky Jun 22, 2012

Hi Adrian! It should be.

Hi Adrian! It should be.

@feryardiant

This comment has been minimized.

Show comment
Hide comment
@feryardiant

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

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.

Show comment
Hide comment
@Namco2k12

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

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.

Show comment
Hide comment
@nguyenvanquan7826

nguyenvanquan7826 Aug 20, 2013

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

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.

Show comment
Hide comment
@verydragon

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

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.

Show comment
Hide comment
@consty

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

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

This comment has been minimized.

Show comment
Hide comment
@alway5dotcom

alway5dotcom Jul 15, 2015

Brilliant 😄

Brilliant 😄

@sean-wang-wenfeng

This comment has been minimized.

Show comment
Hide comment
@sean-wang-wenfeng

sean-wang-wenfeng Nov 1, 2015

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

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.

Show comment
Hide comment
@alex4men

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

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