Skip to content

Instantly share code, notes, and snippets.

@xhacker
Last active October 8, 2020 10:52
Show Gist options
  • Save xhacker/1eb3f87dd81a3c2015e7 to your computer and use it in GitHub Desktop.
Save xhacker/1eb3f87dd81a3c2015e7 to your computer and use it in GitHub Desktop.
Build and run C++ file in Sublime Text 3

How to Install

  • Tools → Build System → New build system...
  • Paste the content below
{
    "cmd": ["g++ ${file} -o ${file_path}/${file_base_name} && echo 'Build Finished' && ${file_path}/${file_base_name}"],
    "shell" : true
}
  • Save as C++ Build and Run.sublime-build

How to Use

  • Open a C++ file
  • Select Tools → Build System → C++ Build and Run
  • cmd + b
@gospodinove
Copy link

It doesn't work for some reason. After I use cin, the program just enter a constant waiting-for-input state. Do you know how to fix this?

@jatzz10
Copy link

jatzz10 commented Jan 10, 2019

@gospodinove Try this ->

{
"cmd": ["g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.cpp",
"working_dir": "${file_path}",
"path": "C:/MinGW/bin", // Path of installed gcc compiler directory
"shell": true
}

It worked for me.

@JadedBeast
Copy link

JadedBeast commented Mar 6, 2019

@gospodinove Try this ->

{
"cmd": ["g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.cpp",
"working_dir": "${file_path}",
"path": "C:/MinGW/bin", // Path of installed gcc compiler directory
"shell": true
}

It worked for me.
I used yours and made few modifications to avoid errors such as using iterators and long long since it's not supported .Thank you for sharing yours , it really helped.
I changed only this :
"cmd": ["g++","-Wall","-ansi", "-std=c++11", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],

@BKumarAtanu
Copy link

Thanks , It works for me.
{
"cmd": ["g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.cpp",
"working_dir": "${file_path}",
"path": "C:/MinGW/bin", // Path of installed gcc compiler directory
"shell": true
}

@Taara-Sinh-Aatrey
Copy link

Taara-Sinh-Aatrey commented Jun 3, 2020

It worked for me but when i am trying to launch wsl instead of cmd then i am facing problem.Actually the wsl launches and before i could see the screen , it closes.So can anyone guide me how can i launch wsl.exe?I have tried to use the below code for launching wsl.
{
"cmd": ["g++","-Wall","-ansi", "-std=c++17", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "wsl.exe", "/k" , "$file_base_name"],
"selector": "source.cpp",
"working_dir": "${file_path}",
"path": "C:/MinGW/bin", // Path of installed gcc compiler directory
"shell": true
}

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