Friday, November 14, 2014

21st Century C

I am reading the book 21st Century C and am learning all sorts of new things. For me personally, here are some highlights and/or discoveries I made directly from the text or because I was poking around after reading the text:

  1. There is such a thing as C11 (and it's awesome). Where have I been?!?
  2. Cygwin is more useful than I realized.
    • While I knew that you could easily install a cross compiler, I didn't realize that you could easily install libraries for the cross compiler using ./configure --host=x86_64-w64-mingw32
    • Cygwin's gdb works with both the Cygwin executables and the MinGW executables.
    • There is a native-GUI version of GNU Emacs called emacs-w32. This would allow one to use the excellent Emacs GDB support with the baked-in Cygwin paths.
  3. GCC has a flag for that
    • I've used -Wall and -Wextra, but I should have also been using -Werror
    • -pg for profiling with gprof (I didn't realize there was even a profiler!)
    • -fprofile-arcs -ftest-coverage for coverage with gcov
  4. Embedding text into a script is called a here document.
  5. pkg-config does a lot of the work for you for CFLAGS and LDFLAGS. Note that Cygwin's MinGW cross compiler has a separate pkg-config executable.
  6. GDB tips and tricks.
    • I've used gdb here and there, but didn't know that in addition to 's' for step and 'n' for next, there is 'u' for "until the next line forward from this." Seems like that would be really helpful for iterating through loops.
    • There is useful syntax for printing arrays, i.e. 'p *array_name@10' to print a dereference of the 10 pointers from array_name
    • You can define macros to easily print structures that you work with. Save your macros in .gdbinit in your home directory.
  7. Doxygen
    • doxygen -g to create an empty configuration file.
  8. fc - fix command shell built-in command
    • By itself, you can edit your last command
    • With the -l option, you can get a list of commands. This can help you create a shell script off of your history.
  9. Z Shell (zsh) is actually worth a look, even if you love using Bash as much as I do.
    • It handles spaces in filenames (for use in for loops).
    • It can do floating-point arithmetic
  10. size_t
    • Can be printf'd with %zu
    • Led me to look up stdint.h and came accross intptr_t, which can hold a pointer.
  11.  GNU Autotools
    • I am planning to avoid these if possible. Hopefully CMake and Friends can get the job done.
  12. Python C Interface
    • Seems pretty simple to do the easy stuff. I would probably still recommend Python's ctypes module first, though.
  13. Type generic macros give C some basic ability to overload functions.

Wednesday, June 25, 2014

Perforce Edit and Revert in Emacs

I found this snippet of Elisp to be helpful for the most basic of Perforce operations:

(defun p4-edit ()
  "Open the file for edit"
  (let ((retval (call-process "p4" nil nil nil "edit" buffer-file-name)))
    (if (eql 0 retval)
    (setq buffer-read-only nil)
      (error "Perforce edit failed"))))

(defun p4-revert ()
  "Revert the file"
  (let ((retval (call-process "p4" nil nil nil "revert" buffer-file-name)))
    (if (eql 0 retval)
    (revert-buffer t t)
      (error "Perforce revert failed"))))

Sunday, April 27, 2014

Making Your Own InnoTab 3 Videos Using mencoder on Linux

Unfortunately, my daughter's InnoTab 2 got into a little accident and went to VTech heaven. My wife upgraded her to the InnoTab 3S. So far, so good, with the exception that her old ripped DVDs no longer work on the new device. The product manual is pretty clear about what kind of video format the InnoTab 3 will support, the problem was just finding an appropriate program for the encoding. Enter mencoder. Here is what I did to make compatible videos using mencoder (with libdvdcss2 installed) on Ubuntu 12.04 LTS:

mencoder \
-nosub \
-oac mp3lame \
-lameopts mode=2:cbr:br=96:vol=7 \
-ovc x264 \
-x264encopts profile=baseline:bitrate=600 \
-vf scale=480:272 \
-o movie.avi \

Friday, January 24, 2014

Using Cygwin and the MinGW Cross Compiler with SDL2

MinGW is currently the most popular way to use GCC on Windows. I recently found that there is a way of using MinGW from within Cygwin as a cross-compiler. I've recently been doing some OpenGL programming across both Linux and Windows, and using Cygwin has allowed me to keep my build system largely the same.
  1. I installed the following Cygwin 64 packages (I'm not sure if they are all necessary)
    1. mingw64-x86_64-binutils
    2. mingw64-x86_64-gcc
    3. mingw64-x86_64-gcc-core
    4. mingw64-x86_64-gcc-g++
    5. mingw64-x86_64-winpthreads
  2. Download the MinGW SDL2 development files.
  3. Extract the SDL2 development files.
  4. Copy source to dest (using SDL2-2.0.1 as the example)
    1. SDL2-2.0.1\x86_64-w64-mingw32\include\SDL2
    2. SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2.a
    3. SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2main.a
    4. SDL2-2.0.1\x86_64-w64-mingw32\bin\sdl2-config
  5. Edit C:\cygwin64\bin\sdl2-config
    1.  Change the "prefix" variable to /usr/x86_64-w64-mingw32/sys-root/mingw
    2. Remove "-XCClinker" from the linker line.
  6. Compile and link (statically)
    1.   x86_64-w64-mingw32-gcc.exe opengltest.c -o gltest.exe $(sdl2-config --cflags --static-libs) -lopengl32

Vim Indentation Based on File/Folder Name/Regex

I've been working between a couple of projects that have wildly different coding standards. Vim has a flexible way to change settings based on a file path regular expression. Here is an example line where I set my indentation to 3 if the file path matches the given regexes (forgive the sprawl across two visual lines):

autocmd BufNewFile,BufRead */projectfolder/*.cpp,*/projectfolder/*.h setlocal expandtab tabstop=3 shiftwidth=3

Wednesday, January 22, 2014

vegan edamame potato salad

I found this recipe on the back of some Bird's Eye shelled edamame a number of years ago. I can't find it anywhere online, so I thought I'd post it myself.

Preparation Time:  15 minutes
Cooking Time:  10 minutes
Total Time:  25 minutes
Serves:  6-8


  • 2 lbs small red skinned potatoes (quartered)
  • 1 package (16 oz) of Birds Eye Shelled Soybeans
  • 3 Tbsp olive oil
  • 2 Tbsp apple cider vinegar
  • 1 Tbsp Dijon mustard
  • 1 Tbsp brown sugar
  • 1 tsp salt
  • 1/2 tsp black pepper
  • 1 clove garlic, chopped (optional)
  • 1/2 red onion, thinly sliced.

Cook potatoes in large pot of salted water for 6-7 minutes or until
tender. Drain and allow to cool. Prepare soybeans per the packaged
directions. Set aside and keep chilled until needed. Mix the next 7
ingredients and whisk to blend. Combine the potatoes, edamame, and
onion with dressing. Mix well. Season with additional salt and pepper
to taste. Chill until ready to use.

Sunday, January 5, 2014

Clozure Common Lisp (CCL) on Raspberry Pi

This is a distillation of a few documents for my own use, including

This is how I was able to get Clozure Common Lisp (revision 15996) running on Raspbian (Wheezy version 2013-12-20 downloaded from
  1. sudo aptitude install build-essential m4 subversion
  2. svn co
  3. cd ccl/lisp-kernel/linuxarm
  4. make clean
  5. make