Hacker Newsnew | past | comments | ask | show | jobs | submit | d00r's commentslogin

I've been using deheader (http://www.catb.org/esr/deheader/) for this.

It detects unnecessary inclusions and also warns about missing headers required for cross-platform compatibility.


I wrote a tool to do this in 10 minutes just several days ago. It iterates over a file, and removes one #include at a time (while keeping all the other headers in place), and each time builds a file. For any successful build, it reports that header.

I used this to prune unnecessary includes throughout the TXR project.

There are some obvious false positives, like includes wrapped in #if/#ifdef and also certain system headers which look unnecessary on one system, but are actually required on others (due to the mess that is called POSIX).

After I applied some of the header removals, I pushed the changes to git and went through all the supported platforms to validate the changes. Almost every platform had some problem which resulted in some header having to be put back: MinGW, Cygwin, Solaris, Mac OS X. For instance, Mac OS is a stickler for needing <signal.h> to declare the kill() function. On other platforms, it also shows up elsewhere, perhaps unistd. On glibc, you get va_list if you include <stdio.h>. Some platforms guard against this; they use some internal typedef name instead of va_list for vfprintf, so you must include <stdarg.h> for va_list. Various problems of this type. So just because a tool tells you, "hey this compiles without <stdarg.h> just fine", that of course means "well, on this system".

   #!/usr/local/bin/txr
   @(next :args)
   @file.c
   @(next `@file.c`)
   @(collect)
   @  (some)
   @lines
   @  (and)
   @    (line linenum)
   #include @header
   @  (end)
   @(end)
   @(require (boundp 'linenum))
   @(do (rename-path `@file.c` `@file.c.bak`))
   @(try)
   @  (do (each ((rem-line linenum)
                 (rem-hdr header))
            (with-stream (s (open-file `@file.c` "w"))
              (tprint (partition* lines (pred rem-line)) s))
            (ignerr (remove-path `opt/@file.o`))
            (when (zerop (sh `make opt/@file.o > /dev/null 2>&1`))
              (put-line `@file.c:@{rem-line}: can remove @{rem-hdr}`))))
   @(finally)
   @  (do (rename-path `@file.c.bak` `@file.c`))
   @(end)
The output is in a form that looks like compiler diagnostics. I can stick it in errors.err and run "vim -q".

This simple tool works in conjunction with the project rule that that headers don't include other headers. The .c files include everything they need in the right order. But the inclusions are added by hand by copy and paste, which can pull in something that isn't actually needed.

You have to iterate the tool. If you remove some header which isn't needed, a second pass can then determine that yet another header isn't needed, because it was only needed by that which was just removed. The iteration isn't worth building into the code.


The tool jacobolus is referring to is a color scheme editor I made: https://www.colorize.io/baskerville/RVXGW50G/.

It was, indeed, originally based on HuSL, but I later realized I could get a more meaningful picker by just constraining the chroma component not to go beyond the maximum in-gamut chroma for the given lightness and hue.



One of the things that will be harder to justify is the use of raw white text on raw black background.


Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: