# How to Build C++ Projects with STDIN in Sublime Text 3

### 2017-10-10

I’ve been a die-hard fan of Sublime Text (ST), all the time from the 2nd to the 3rd version now. To be frank, I use ST in almost all situations: taking lecture notes in LaTeX, drafting small projects in Python or R (while for large projects, Jupyter Notebook and RStudio are stronger platforms), or tracking my Todo-list in Markdown. Everything works so smoothly and lightly with ST, especially compared with IDEs like Xcode or Visual Studio. On the other hand, it is more friendly to users who are not so keen on shortcuts -- I mean, shortcuts for everything, like Vim or Eclipse (there are abundant shortcuts in ST already and that’s enough for me). It must be a different world if I mastered that technique, but for now… I think I’m fine with a large and smart touchpad on my MBP1.

However, there’s one thing missing in ST3, and that is C++ project building. To be specific, there’re two features that are not implemented yet:

1. The only build system for C++, is called C++ Single File, and you know what that means. The only scenario that works is when you just have one file suffixed by cpp. No hpp or other cpps, no.
2. You cannot input through STDIN because the editor freezes when cin is met. No input, no.

These two missing features are really annoying at first when I need to write a project in C++ and do some debugging. However, eventually I managed to write my own build system for C++ which accepts multiple files (including head files) and even inputs. I’ll just skip the steps to create a new build system as they can be easily handled if you check the menu. The codes for the system is as below:

{
"shell_cmd": "g++ *.cpp -I /opt/local/include -o a && open -a Terminal.app ./a",
"selector": "source.c++, source.cxx, source.cpp",
"working_dir" : "\$file_path"
}


The basic idea is to build2, create a executive program named a3 and then run it in the Terminal, where STDIN is possible.

I hope this post can help anyone who need this trick.

1. There’s actually one more text editor (not IDE) I’ve tried: Atom. It’s developed and maintained by GitHub and looks really fancy at very first glance. However, at least on my MBP, it’s not as fast as ST3 and was later knocked out. ↩︎
2. Note here I included a parameter -I /opt/local/include in case external libraries like boost are required in the code. ↩︎
3. Instead of the base-name of the file, I name this executive program as a simple a because when there’re multiple cpp files in the project, it’s hard for the computer to locate which one contains the main code and determine a name after that file. In many cases, the cpp file that is active when you hit command+B only declares some classes, but the executive program is named after it. That’s the actual case I encountered and want to eliminate. ↩︎