Here are my notes on setting up an Emacs C++ development environment. I'm using GNU g++ (4.3.3), GNU make (3.81), GNU gdb (3.8-debian), and GNU emacs (23.0.92.1 built from CVS 2009-04-22) on Ubuntu Linux (9.04 Jaunty Jackalope).

Install tools

If you don't already have g++ , make , and gdb , install them:

sudo apt-get install g++ make gdb

Create files

Create a project directory: mkdir helloworld

Create a source file helloworld/hello.cpp : #include <iostream> int main(void) { char greeting[] = "Hello World!"; std::cout << greeting << std::endl; return 0; }

: Create a makefile helloworld/Makefile : # Makefile for GNU g++ CC=g++ CFLAGS=-g -Wall all: hello hello: hello.cpp $(CC) $(CFLAGS) -o hello hello.cpp clean: rm hello

Compile within Emacs

See also: 32 Compiling and Testing Programs in the GNU Emacs manual.

Open hello.cpp in Emacs

in Emacs M-x compile RET make -k RET Note: The default compile command is make -k . You can change the default command by setting the compile-command variable in your .emacs file.

Useful commands in the compilation window: C-x ` : Go to next error in the code M-n : Go to next error message M-p : Go to previous error message RET : Go to the source code for the current error message



Run using Emacs shell

M-! ./hello RET You should see the output in the minibuffer.

Debug with gdb in Emacs

For more information see: 32.6 Running Debuggers Under Emacs in the GNU Emacs manual.

To show multiple debugging windows such as breakpoints, locals, etc., set the gdb-many-windows variable in ~/.emacs : (setq gdb-many-windows t) Restart Emacs.





variable in : Restart Emacs. Start the debugger. While visiting helloworld/hello.cpp : M-x gdb RET --annotate=3 hello RET

: Set a breakpoint by clicking in the left margin at the desired location.

Run the debugger by typing run at the (gdb) prompt in the *gud-hello* buffer.

at the prompt in the buffer. In the GUD buffer, use the following commands: C-c C-s Step into C-c C-n Stev over C-c C-p Evaluate the expression at point. C-c C-r Continue C-c C-u Continue until current line



When finished, type quit at the (gdb) prompt.

Documentation:

References