25

I'm just beginning to learn programming (on C++ and Python), and by beginning I mean total beginning ("hello world" beginning...). Not wanting to use multiple IDE's, I would like to be able to code and build–simple–programs with my text editor, Sublime Text 2. Could someone indicate me, with a step-by-step tutorial, how to implement C++ and Python compiling and executing capabilities in Sublime Text.

I've searched Sublime Text build systems on the site, but the answers are very specific and can't help a rookie like me (but they'll probably help me later).

Thanks

guillaume8375
  • 499
  • 1
  • 4
  • 10

4 Answers4

43

Sublime Text 2 already comes with scripts for building and running Python and C++ programs.

Simply press Cmd+B (or Ctrl+B on Windows & Linux) when a .py or .cpp file is open. The Python file will automatically execute and show the result in the built in console.

For C++, you need to press Cmd+Shift+B (Ctrl+Shift+B on Windows & Linux) to run it after building.

You need to have Python installed (get it here for Windows), and also a C++ compiler. The build system for C++ tries to call g++ by default (get it here for Windows. Remember to select the C++ compiler when installing!).

You will need to add the directories to path (c:\python32\ or similar for python, c:\mingw\bin or similar for the C++ compiler).

On windows, you may experience problems running the C++ programs (it tries to use bash). But Ctrl+B builds the program, and you can then use a command line to run it. Python works flawlessly on Windows.

Chris
  • 332
  • 3
  • 8
DkM
  • 800
  • 1
  • 7
  • 22
  • @sergey I've just tried your answer with Python (print "Hello, World!" ) but it didn't work. Here is the message I get from Sublime Text: [Decode error - output not utf-8] [cmd: [u'g++', u'D:\\essai.py', u'-o', u'D:/essai']] [dir: D:\Google Drive] [path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Samsung\Samsung PC Studio 3\] [Finished] – guillaume8375 Jul 30 '12 at 21:26
  • @guillaume8375 I suppose you mean me? Anyway, I added some more to my explanation. You need to install python from the link provided, and then you need to add the install directory to your path! Then restart sublime and ctrl+b should work. ALSO notice that the file you work on in sublime need to have the .py extension, otherwise sublime text cannot know it is a python file (you can also specify manually, but try naming the file correctly) – DkM Jul 30 '12 at 21:31
  • 1
    Thanks a lot. I installed Python but Idon't know how to add the install directory to my PATH. – guillaume8375 Jul 30 '12 at 21:36
  • @guillaume8375 Here is a link explaining how to do: http://www.mathworks.se/support/solutions/en/data/1-15ZLK/index.html except you just add ;c:\python32 or so(python install dir) instead of ;c:\matlab\bin – DkM Jul 30 '12 at 21:38
  • 1
    It worked! Thanks a lot, you and @sergey have been very helpful! – guillaume8375 Jul 30 '12 at 21:50
  • But is it possible to pass arguments when I run my built `.exe` file? Now I can only run an `.exe` with `Ctrl+Shift+B` but cannot pass any args. Is there a possibility in ST2 to pass args? How? – Green Mar 13 '13 at 10:16
  • how to take console input in C++ files – dorado Sep 09 '15 at 18:43
2

windows(install minigw, python2.7 and added to the system path)
cpp:

  1. build: ctrl+b
  2. run: ctrl+shift+b

python:

  1. build and run: ctrl+b

you may try to learn the the .sublime-build files in your Tools -> Build system -> New build system

Highmastdon
  • 6,960
  • 7
  • 40
  • 68
hustljian
  • 965
  • 12
  • 9
1

So, you don't want to use an IDE but then you want IDE features from a text editor? :)

Most people who use a text editor for writing code use terminal to build and run the code.

So, for C++, the instructions are:

make (or gcc myprogram.c)
./myprogram

for a Python program, it's even simpler:

python ./myprogram.py

If you're not comfortable with terminal, then you probably need an IDE.

Sergey
  • 11,892
  • 2
  • 41
  • 52
  • 2
    I know that IDE are recommended for beginners, but the thing is I want to be able to build C++ and Python programs with the same application, yet it seems that every IDE is tailored to a specific language. By the way, I forgot to mention that I'm using Windows 7 64 bit and that I don't know what files or programs to install to be able to build. I really am THE newbie :-) – guillaume8375 Jul 30 '12 at 21:12
  • 1
    I wouldn't say that using an IDE is better for a beginner, I actually think that using a text editor is a good choice since it allows you to learn how things actually work. What I was trying to say is that a program is just a text file you can create in any text editor, and to build it you can directly invoke your compiler from command line (for C++). Python, being an interpreted language, does not require "building" at all, you just directly invoke the script from the terminal. SublimeText's build function is in no way required in order to do that. – Sergey Jul 31 '12 at 00:55
1

for c++ I actually made sublime to produce colorful error messages which are easier to read and you can also click on the errors which takes you to the file with the error.

You can look at how I modified the build to do what I wanted in here

Community
  • 1
  • 1
Mani Zandifar
  • 1,238
  • 11
  • 14