Emacs: the Free Software IDE
Now for one of the really fun things about Emacs: Electric C. Press certain characters, and Emacs takes care of the indentation and the pretty printing for us. Having completed the function declaration, in a normal editor one would press Return, type a left brace ({), then press Return and Tab. Not in Emacs; just type a left brace and Emacs does the rest. (If it doesn't work the first time, undo the brace with Ctrl-_, then press Ctrl-C Ctrl-A to toggle electrification.)
Now we type in a line of code. Type in printf, then a space. Now it's time for a parenthesis. While we're at it, to keep our parentheses balanced, it's also time for a closed parentheses. So press M-(, that's Alt-( on a PC keyboard. Emacs puts in the pair of parentheses and moves back a character so we can type code inside. And, we type in "Hello, wolrd.\n", typo and all (we'll come back to that).
Instead of pressing Return, we enter another electric character, Ctrl-J. The astute reader has already noticed that we left out a semicolon. Watch what happens. Ctrl-J should move us down a line and indent to just below the “p” in “printf”. In this case, it doesn't. Emacs assumes that we are continuing from the line above, so it indents further. Even to a software engineer who has only had his first cup of coffee for the day, this should be an alert that something is wrong. Yes, indeed, we missed our semicolon. We undo the Ctrl-J with Ctrl-Shift-- (that's Control-Shift-Hyphen) and type in the semicolon. Hey! Semicolons are electric, too! But notice how Emacs prevented a bug from happening? That's even cooler!
Now, of course, it's time to enter a right brace in order to end the function main. Because our electric semicolon indented for us, do we have to backspace back to the left column? Nope, just type the right brace.
Now that we have our program written, we save it (Ctrl-X Ctrl-S) and run a test compile. Since this is a single-source file program, we don't need a make file. (But there is a make file mode, which I will leave as an exercise for the student.) Instead, we compile from within Emacs, M-X compile. This results in a proposed compile command line, in this case the default make -k. Since we don't have a make file, we use the down arrow key and write our own compile command:
gcc -g -o hello hello.c
The -g option compiles in information for GDB, the GNU Debugger, and the -o option tells the linker that the output file is to be named “hello”. And we get, much to our amazement, a perfect compile (Figure 3).

Figure 3. A Successful Compile in Emacs
Now to test it. Feeling brave—or lucky—we fire up a shell under Emacs with M-X shell and execute the program (Figure 4). However, something isn't quite right. We misspelled “world”. Oops.

Figure 4. Executing the Program from a Shell inside Emacs
But rather than just correct that error, we can spell check our comments and strings. To do that, we go back to the source buffer with Ctrl-X Ctrl-B Return. Now either use the pull-down menu (Tools—>Spell-Checking—>Spell-Check Comments) or do M-X ispell-comments-and-strings (now is a good time to see the Sidebar on Tab completion). We press the spacebar to bypass IDE and 0 to accept world instead of wolrd (Figure 5). Then Ctrl-X Ctrl-X to save our changes.

Figure 5. Spell Checking Source Code in Emacs
Now that we have that done, it's time to compile again. Like Bash, Emacs remembers our command history, so M-X followed by one or more up arrow keys gets us back to the compile command. Press Return. Emacs shows us our last compile command line. It looks good, so press Return again.
Emacs is also a front end for GDB. To enter GDB mode, do M-X gdb. Emacs comes up with a proposed command line, “gdb”. We add the name of the executable file to debug, hello, and press Return. This tosses us into gdb mode, with the gdb prompt ready for action. We set a breakpoint at the function “main” with the gdb command break main. (Tab completion works here, too.) Then type run to run the program to our first breakpoint.
GDB shows us the input values to the program. Emacs, however, has the other window set up to show the source. As we step through the program, Emacs will indicate the next line to execute with an arrow in the source window. We can even switch buffers (Ctrl-X Ctrl-O) and edit the source as the fancy strikes. Pressing N executes one statement. In this case, that statement produces output, which we see in the GDB window. One more N and GDB informs us that the program exited with a return value of, well, garbage.
We recall that the successful completion of a program should generally return zero. We should add that to the program. So we quit GDB and switch to the source-code window. The cursor is just in front of the final brace of the program. To add the return code, we enter return (0).
We won't get very fancy in our show-and-tell. But for anything more sophisticated than what we do here, you will find a lot of repetition before getting to the interesting part of the program execution. Therefore, you can prepare an init file for GDB, which is a list of GDB commands that are executed in the order GDB encounters them in the file. So you can automate anything GDB can do—which is quite a lot.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Designing Electronics with Linux
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- General
9 min 30 sec ago - Kernel Problem
10 hours 12 min ago - BASH script to log IPs on public web server
14 hours 39 min ago - DynDNS
18 hours 15 min ago - Reply to comment | Linux Journal
18 hours 47 min ago - All the articles you talked
21 hours 11 min ago - All the articles you talked
21 hours 14 min ago - All the articles you talked
21 hours 15 min ago - myip
1 day 1 hour ago - Keeping track of IP address
1 day 3 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Emacs 22 reference card
An Emacs 22 reference card - PDF generated from the refcard.tex TeX source file that comes in the Emacs distribution.
A GUD (GDB) tip
You forgot to mention in regards to GUD (GDB) that C-x SPC will set a breakpoint on a given line.