//
you're reading...
Uncategorized

Learn Emacs for C++ Software Development

First things first:

Install Emacs for Windows, Mac, and Linux:

  • OS X: Install vanilla Emacs as a Mac app. There are other options, like Aquamacs, which are supposed to make Emacs more “Mac-like”, but they’re problematic in the long run because they’re set up so differently from standard Emacs that it’s difficult to use the Emacs manual or follow along with tutorials
  • Linux: A recent version of Emacs is available directly from the package manager of most Linux distributions. For Ubuntu, follow these instructions.
  • Windows: You can find installation instruction on this page. Alternatively, you can install Emacs with a single command using chocolatey, but I have not tried this.

Once installed and running you should have a window like this:

Screen Shot 2015-01-25 at 7.29.57 PM

Most commonly used commands are accessible through dropdown menus; go check them out now, but be aware that many advanced users rely more on keyboard shortcuts. Check out this Emacs Reference Card to see the organization of these keyboard shortcuts. Most commands begin with holding down the control key C- or the meta key M- (alt or escape keys).

Aside: Emacs is inifinitely customizable by modifying .emacs configuration file. You may ignore this for now, but in future you may want to customize, so see here for more details.

Installing  C++/g++

We will be using the open source GNU g++ compiler for the c++ programming language.

  • OS X and Linux: These OSs should have a version of g++ already installed.
  • Windows: You can find installation instructions on this page.

Running g++ within Emacs

Now let’s see how Emacs can help us with writing, editing, and eventually debugging C++ code.

Download the program sum1.cpp into your current directory.

Load the file, choose Open File… from the Emacs File menu. When it prompts you with:

Find file: curr-dir/

(where curr-dir is your current directory), type in the name of the file, giving:

Find file: curr-dir/sum1.cpp

and press <Return>.

Aside: The tilde (~) is shorthand for your home directory in many programs and at the UNIX prompt. Thus, tilde (~) will appear at the beginning of what Emacs lists as your current directory if it is under your home directory.


You should now see the source code in Emacs. Note that since we have loaded C++ code, Emacs has gone into C++ mode. Emacs knows it is editing C++ code because the filename ends in .cpp.


C++ Menu

Note that when you are in C++ mode in Emacs, there is an additional menu named C++, with commands for navigating and editing C++ programs.


Let’s use one of the C++ menu’s commands. In the Emacs window, put the cursor at the end of the first line that uses cout:     cout << “Enter how many numbers I will sum: “;


If you now choose Forward Statement from the C++ menu, it will go to the next statement, which is the one using cin:       cin >> howmany;

Aside: Roughly, a statement is something that ends in a semi-colon (;), which is a simple statement. There are also compound statements–all of the stuff in a block (i.e., between braces, { and }) forms a single compound statement.


You might use the Forward Statement (M-e) command to advance through statements in a program when you cannot easily tell where statements begin and end. The Backward Statement (M-a) command retreats back .

Automatic Indentation

Another feature you can use when Emacs is in C++ mode is automatic indentation. You can use this before or after the fact. In other words, you can press the <Tab> key before you type a line of code for it to indent to the proper place. Or, you can press the <Tab> key while on a line, to fix its indentation after the fact.

To compile while in Emacs:

Use the Emacs command

<Esc> x compile

At the bottom of the Emacs screen, it will ask you what UNIX command to use for compilation. The default is make -k (make is a separate topic), so erase that and type whatever compilation command you’d use at the UNIX prompt:

g++ -o total sum1.cpp

Note that we told g++ to name the executable total.


Aside: The code you are compiling should be in the same directory as the one in which you started up Emacs.


Note that Emacs has now split its window into 2 parts (or subwindows), one of which contains the output of the attempt to compile the program:

 Screen Shot 2015-01-25 at 8.49.57 PM

There are errors in the compilation, and we can click on them and we will be brought to those line numbers of the source code to make appropriate edits.

Learning Keyboard ShortCuts:

The Emacs Tutorial or
Jessica Hamrick’s Absolute Beginners Guide to Emacs

Using Emacs for Programming C++

 

More References:

Continue on your Emacs journey:

My Notes:

Emacs can be run on different platforms using same or different configurations.

  • For Clojure Programming I run with configuration file setup found here.
  • For C++ I run with plain version using terminal command:
    $open /Applications/Emacs.app –args -q
Advertisement

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: