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.