Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build and run c++ programs in Sublime Text 2, Windows 8?

I installed Codeblocks with mingw, chose default compiler, and could build and run a simple hello program without errors.

I installed Sublime Text 2, copy pasted the same hello world program:

// my first program in C++
#include <iostream>

using namespace std;

int main ()
{
    cout << "Hello World!";
    return 0;
}

Upon building, I get the error message:

[Error 2] The system cannot find the file specified
[cmd:  [u'bash', u'-c', u"g++ '' -o '/' && '/'"]]
[dir:  C:\Windows\system32]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\]
[Finished]

What do I need to do in order to build and run a simple program using Sublime Text 2?

like image 730
Leonardo Lopez Avatar asked Oct 30 '12 18:10

Leonardo Lopez


People also ask

How do I run C code in Sublime Text windows?

JSON for Package New Build Package in Sublime Text So, we have to add a new build system. We have to go Tools > Build System > New Build System and then add the following code and save the file: Finally, We can now build our code by pressing Command + B and see the outputs for the inputs of in. txt file into out.

How do I run a program in Sublime Text 2?

To run code in Sublime Text, go to Tools > Build System, and select the language for your code (Sublime comes with support for various languages like Python, Ruby, Bash, and more). Next, press Cmd+B on Mac or Ctrl+B on Windows to run your code.

Can you compile C in sublime?

You can compile your C++/Java programs in vim as convenience as Sublime Text or NotePad++ with some dedicated plugins for Vim 8 or NeoVim.


3 Answers

(I assume you already have installed MingW in your computer.)

You need to go to Preferences->Browse Packages->C++ folder->C++.sublime-build; bring this C++.sublime build file into the sublime text editor and now paste this code :

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

Hope this helps you.

like image 80
pharask Avatar answered Sep 17 '22 15:09

pharask


First, make sure you save the file you're working on, wherever on your drive, before building and running.

Sublime Text 2 needs g++, bash, etc in order to compile. These packages need to be installed on your computer, as per the instructions on this page:

http://mjiang.com/mec/cs244/files/Installing%20c++_g++_on_Windows.pdf

like image 40
Leonardo Lopez Avatar answered Sep 16 '22 15:09

Leonardo Lopez


For WINDOWS:

If you have Dev C++ (Bloodshed) then,

OPEN SUBLIME TEXT 2 and creat a new file to write your code (change build system to c++ through Tools> Build System> C++ as SublimeText2 doesn't come with build-system for c)

After that, you save that file to bin folder contained in Dev-Cpp folder and press ctrl+b

If your code is correct (bug free) then you'll found a corresponding file (in .exe format) on same directory which will show you

Hello World!

REMEMBER: SUBLIME TEXT 2 is an Editor, not a COMPILER

like image 26
Biswajit Paul Avatar answered Sep 18 '22 15:09

Biswajit Paul