tags:

views:

938

answers:

12

I'd like to study some good c/c++ code. The code should:

  1. be good in style and structure, presenting best practices
  2. be a real life program (not an example or toy)
  3. not too big so it doesn't takes ages to analyse it
  4. Windows and/or Unix

I know there are 1000s of open source projects out there. But I'd like to hear your suggestions.

I am Java programmer and I am curious how good project in c/c++ is structured (files), granularity of classes/functions/files, documentation/comments, build tools etc.

Multiple suggestions are welcomed e.g. this is good windows example, that is good pure C etc.

+6  A: 

I would specifically mention memcached. It's a great example of fairly short, readable code with a clear purpose.

Second, I would recommend the Apache web server. It's a fantastically well-run open source project that you'll learn a lot from, both about the language, as well as general design practices and networking/threading.

Tony k
A: 

Here's the main file for relayd, which is a daemon that handles load balancing and failover. It's nicely written, full featured, clean C code. It's big enough that you get a feel for a "real world" program, but not so big that you can't get your head around it with some work. Great for secure, well written networking code, daemons, etc.

Modesty aside, there's also a little tiling window manager for X that I've helped with. It's quite small but it's a real wm, and the other coders on it have high standards (and hold me to them!)... Nice for learning basic X stuff.

dwc
+2  A: 

Samba: a large, cross-platform (POSIX-based), pure C, very popular network server.

Samba has good developer documentation, and the code base itself is very well organized; I've had no problems diving into just about any location within the code to answer a question I had about its inner workings or to track down a bug.

Josh Kelley
+3  A: 

I find the implementation of Lua is quite nice C source. But languages are a bit odd as an example project.

Keith Nicholas
Many historical source kits (including the current 5.1.4) are at http://www.lua.org/ftp/ and there is a web interface to the current source kit at http://www.lua.org/source/5.1/
RBerteig
+2  A: 

Diomidis Spinellis has some suggestions in his book, "Code Reading: The Open Source Perspective", Addison Wesley, 2003. ISBN 0-201-79940-5.

Perhaps the book could be useful to you.

URL http://www.spinellis.gr/codereading/

Hideo
+1  A: 

Google's Chromium is pretty huge, but you don't have to analyze it all to appreciate its clean, peer-reviewed structure. Also, it's a good example of nicely done developer environment deployment.

macbirdie
thanks for the suggestion! I can only mark one answer as 'accepted', nevertheless your suggestion is the 3rd project I am going to look into.
qKoder
A: 

I'd vote for nginx: http://sysoev.ru/en/ as an example of a very good C programming style

Konstantin
A: 

You definitely need to choose between C or C++. The obvious example: good practices for error management differ a lot.

Jem
A: 

As for C++ I would recommend you the Qt library sources and KDE's source code and its programs.

It's beautifully written code and the way that you should program in C++.

nmuntz
A: 

This one Apache log4cxx

lsalamon
A: 

K&R has many good examples of real programs. For instance, on page 115 of the Second Edition there are two simple implementations of the echo command. From there, it might be interesting to see how the command was implemented over time. For instance, there is a version in the GNU coreutils source. A version derived from BSD can be found in BusyBox source.

It might be interesting to see how the newer versions compare to the textbook examples. Why are the programs that are used in real life longer? How much of the code is related to new features and how much is related to dealing with edge cases? How would you write the same code in Java?

Jon Ericson
A: 

More C++ suggestions in: Examples of “modern c++” in action?

Dan