Notes on C++ development with Emacs on Ubuntu Linux
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.cppin Emacs -
M-x compile RET
make -k RET
Note: The default compile command is
make -k. You can change the default command by setting thecompile-commandvariable in your.emacsfile. - Useful commands in the compilation window:
C-x `: Go to next error in the codeM-n: Go to next error messageM-p: Go to previous error messageRET: 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-windowsvariable in~/.emacs:(setq gdb-many-windows t)
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
runat the(gdb)prompt in the*gud-hello*buffer. - In the GUD buffer, use the following commands:
C-c C-sStep intoC-c C-nStev overC-c C-pEvaluate the expression at point.C-c C-rContinueC-c C-uContinue until current line
- When finished, type
quitat the(gdb)prompt.
Documentation:
References
- Practical C++ Programming, 2nd Edition by Steve Oualine (2004)
- Learning GNU Emacs, 3rd Edition by Debra Cameron et al. (2004)
- Emacs: the Free Software IDE at the Linux Journal (2002)
Related posts
- Colorized, interactive "git blame" in Emacs: vc-annotate — posted 2011-05-28
- My Emacs Python environment — posted 2010-05-10
- Emacs espresso-mode for jQuery — posted 2010-03-10
- Creating remote server nicknames with .ssh/config — posted 2008-11-20
- Emacs notes — posted 2008-11-03
3
Comments
—
Comments feed for this post
#2 Eliot commented on 2009-07-20:
Alex, Thanks for the links to Cedet and ECB. I had heard of ECB, but didn't look into it very thoroughly. It looks like it could be very useful. The code completion in Cedet looks especially interesting. Though Emacs' dabbrev-expand is quick and useful, sometimes I need something more robust. Maybe Cedet's code completion is what I need! I will check it out.
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(5)
-
aws
(9)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(11)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(5)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(144)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(5)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 Alex Ott commented on 2009-07-19:
Cedet and article about it - for more comfortable work with code
ECB - emac code browser