Skip to content

Instantly share code, notes, and snippets.

@jdhao
Last active September 1, 2023 09:59
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jdhao/3e45ba9164fe3d27c40ea86b6455435f to your computer and use it in GitHub Desktop.
Save jdhao/3e45ba9164fe3d27c40ea86b6455435f to your computer and use it in GitHub Desktop.
Sublime text 3 C++ build system to run C++ executable within sublime text or in a terminal emulator in cases that the program need input from standard input. This build system works on Linux, Windows and MacOS
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
"variants":
[
{
"name": "Run in Terminal",
"linux": {
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name}; bash'", // for xterm
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -hold -e ${file_path}/${file_base_name}", // for xterm
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && konsole --hold -e ${file_path}/./${file_base_name}", // for konsole
},
"windows":{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k $file_base_name "
// "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start \"$file_base_name\" call $file_base_name"
},
"osx":{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
},
"shell": true,
},
]
}
@hedo95
Copy link

hedo95 commented Jan 18, 2019

Hi,

I use OSX and have to copy and paste this one or on the script should delete "Linux:...." and "Windous:......" ?

Thnk u!

@jdhao
Copy link
Author

jdhao commented May 17, 2019

Hi,

I use OSX and have to copy and paste this one or on the script should delete "Linux:...." and "Windous:......" ?

Thnk u!

No, you do not need to. Sublime Text will recognize your platform and use the correct command.

@Kkuntal990
Copy link

What could we write in konsole script for echo and exit just like you did in xterm?

"&& echo && echo Press ENTER to continue && read line && exit'",

@jdhao
Copy link
Author

jdhao commented Aug 11, 2019

What could we write in konsole script for echo and exit just like you did in xterm?

"&& echo && echo Press ENTER to continue && read line && exit'",

There is an option for konsole in the above settings. Just uncomment it and comment the line about xterm.

@dev-noob
Copy link

its showing permission denied

@jdhao
Copy link
Author

jdhao commented Jul 18, 2020

its showing permission denied

Then solve it. Google is your friend.

@stilghar
Copy link

stilghar commented Aug 7, 2020

Thank you so much. I just swapped the xterm for gnome-terminal and it works, im still new to linux environment but nonetheless you help me so much cheers!

@stilghar
Copy link

stilghar commented Aug 8, 2020

is there a way to solve this warning?

Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

Use “-- ” to terminate the options and put the command line to execute after it.

@madhur1846
Copy link

i use macOS and its showing error xterm not found any suggestion?

@jdhao
Copy link
Author

jdhao commented Sep 14, 2020

i use macOS and its showing error xterm not found any suggestion?

xterm is not shipped with MacOS. You have to use another terminal application.
I am not familiar with MacOS. But I suppose you can run terminal.app from the command line. It is not much different from running xterm.

@cadros
Copy link

cadros commented Mar 6, 2021

i use macOS and its showing error xterm not found any suggestion?

use open -a Terminal.app "${file_path}/${file_base_name}" instead

Copy link

ghost commented Oct 2, 2021

is there a way to solve this warning?

Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

Use “-- ” to terminate the options and put the command line to execute after it.

you can use command like this, it won't show this warning

"shell_cmd": "g++ -std=c++17 -Wall \"${file}\" -o \"${file_path}/${file_base_name}.out\" && gnome-terminal --geometry=55x52   --tab --active -- bash -c 'bash -c \"${file_path}/${file_base_name}.out&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal

@TheOnlyFastCoder2
Copy link

but, you need to install terminus :
{
"target": "terminus_open",
"cancel": "terminus_cancel_build",
"shell_cmd": "g++ ${file} -o ${file_path}/${file_base_name} && ${file_path}/${file_base_name}.exe && pause ",
"file_regex": "^(..[^:]):([0-9]+):?([0-9]+)?:? (.)$",
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
}

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