ursus maritimus bio photo

ursus maritimus

"passionate and serene, profound and simple, affectionate and proud, subtle and straightforward"

Recently I’m converting from a clan called Java back to something else called C++, in which I believed so profoundly three years ago.

The past three years witnessed more changes apart from the my religion for programming languages, but also my whole programming workflow as well.
I’m now a OS X user and a command line enthusiast, while three years ago I was still struggling between Windows & Ubuntu and was an IDE advocate (by the way I still find Visual Studio 2010 the best C++ IDE I’ve ever used).

So there is gonna be some change. Let’s get going.

Environment

The environment comes with the package Xcode Command Line Tools. It has the compiler, library, debugger… everything essential a system should provide to program C++. That being said, I personally recommend installing the full Xcode package, since at times you’ll need more than that. Yes it takes around 7 gigabytes of disk, but to make your life easier, it worths that space.

As you may expect, the toolkit is contained in the full Xcode package. But for Xcode 4.3 and later, it is no longer automatically installed, so don’t assume you have installed it if you saw Xcode on your machine.

So how do you know you have already installed Xcode command line tools? As introduced in this very nice tutorial and my own experience, there are three options:

Check if you have installed Xcode full package. Just type:

xcode-select -p

to your terminal. If you see the following line:

/Applications/Xcode.app/Contents/Developer

It means you have installed the Xcode full package, and you are good to go. If you instead see this line:

/Library/Developer/CommandLineTools

It means you have installed the Xcode commnad line tool, you are also good to go.

Or you can type gcc to your terminal, you might see the prompt that say something like “gcc requires you to install command line developer tools” and ask if you want to install. Just say yes, and after the installation process you’ll have the developer tools package in your machine.

You can also open the Xcode application and go to the preferences window. Proceed to the download section and if you see a install button next to the command line tools, it means you haven’t install the command line tools or at least your tool is not up to date. Press the button to install/update the tool and you are done.

Editor

OK here is the embarrassing moment: I use Vim myself, and so for those Emacs users, I won’t teach you how to set up editor unless you convert to Vim :)

For Vim users, I assume you have the basic plugins such as NerdTree, Vundle and Syntastic (if you don’t know what I’m talking about I suggest you to look at this and do a little personalization with your vim first). What I would like to recommend here for C++ programming are these two plugins: YouCompleteMe and ctags.

Under Mac OS X, you may want to use MacVim instead of Vim, since it often comes with most complete support customized for Mac OS X (such as python). You can use it like gVim in a graphical interface, or invoke mvim -v to use it within the terminal. You can even mimic Vim by setting an alias alias vim='mvim -v'.

YouCompleteMe (YCM)

Trust me – I’ve tried many, many different auto-completion plugins in Vim, since the time when I was still sticked to Java. The story is, most of the plugins claim they can do something amazing, but they either just suck or take forever to get the configuration correct (at least for me). YouCompleteMe is the first and the only auto-completion plugin that I managed to set up correctly on my Vim and that makes me feel it gets its work done.

The setup for YouCompleteMe is somewhat tricky, and at times its documentation is not perfect. To avoid diverging too far in this post, I set aside an individual post for the procedure. If you are interested, read it here.

ctags

ctags is a must-have if you often deal with large projects. Strictly speaking it is not a Vim plugin, in fact you can use it with Emacs or gedit. But since Vim is the best editor in the world, you should use it with Vim ;)

For the definition of ctags, let me quote the definition from the official website:

Ctags generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object).

These tags will be generated into a file called tags (by default). When you search for the definition of a variable / the invocations of a function, Vim will read this tags file and point you to the place(s) you should look at. That’s why it is especially useful for large projects – you’ll need to figure out what variables / functions actually do from time to time.

To start using ctags, download the source package from the official website. cd to the directory of your source package and run the following script:

./configure
make
make install

If you are working on a server, you may want to substitute the first line with:

./configure --prefix=/path/to/a/local/directory

so that you don’t have to fiddle with the sudo previlege.

After installation, check your version with ctags --version. If you see something start with Exuberant Ctags, you are fine. If not, you probably have some older version ctags and you are still running that older version. So you may probably need to remove the older version before install again, or overwrite your PATH variable to overwrite the path to your older version.

To generate tags file, most often you’ll cd to your codebase and run:

ctags -R .

The -R option tells ctags that you want to search recursively into the sub-directories of your codebase.

Now you have the tag file for your codebase, you can start to code in Vim. To search for a tag, move your cursor to a function / a class / a variable name and type ctrl + ], and you’ll be tranferred to the first tag in the list. If it is not desired, type g + ctrl + ] instead to see the whole tag list. To return from the transferred postion, type ctrl + t.

Compiler

If you set up the environment (Xcode Command Line Tools) correctly, you should have both clang and gcc. I personally slightly prefer gcc for more complete language support, more optimization options and better compatibility for some projects. But I’ve also heard that clang might generate more detailed error messages. It’s up to you which to choose.

Note that if you use YouCompleteMe, you must have clang ready on your machine, since it has a clang-based engine.

Debugger

Save yourself the endless trouble and use lldb instead of gdb. lldb defaults to the OS X environment and is very similar to gdb except some differences in commands. In case you are wondering, here is a very useful chart to show these differences. You can also use this chart as a cheat sheet for common operations.

To start debugging an executable, type lldb /path/to/executable to your terminal to start the debugger. Setup the breakpoints and other stuff and type run to actually start the program. You can add any argument you want to pass to the program after the run command.

Two more tips with the debugger in case you are new to command line workflow: remember to compile your program with -g option to enable variable inspection; the command to exit lldb or gdb is quit ;)



I understand that this tutorial is error-prone and by no means complete. If you have any suggestions on how to make this tutorial more practical, leave a note in the comments and we can discuss!