How to build and run c++ programs in Sublime Text

2019-02-03 09:50发布

问题:

As a novice programmer, I have installed Codeblocks with mingw (dont really know what that means), chose default compiler, and could build and run fine.

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

// my first program in C++

#include <iostream>

using namespace std;

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

I then build, get 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 run a simple program using Sublime Text 2?

回答1:

Solved. 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://www.claremontmckenna.edu/pages/faculty/alee/g++/g++.html

IMPORTANT: Make sure you save the file you're working on, wherever on your drive, before building and running.



回答2:

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



回答3:

You could use my working C++.sublime-build file for Windows: https://gist.github.com/trietptm/4950038



回答4:

just create new Build-system (TOOLS->BUILD SYSTEM->NEW BUILD SYSTEM)

{
"windows":
{
"cmd": ["g++", "$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
},
"selector": "source.c++",
"shell": true,
"working_dir": "${file_path}"
}

and save it as (name_you_can_provide).sublime-build and use that build system. :)



回答5:

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



回答6:

You must install MinGW, then add path to MinGW to PATH variable.