c

Shebang for C .... Almost

Shebang for C .... Almost

Bradley Noyes published on
3 min, 527 words

Shebang for C ... Almost🔗

Sometimes I need to write a small C program that lives as a single C file. It's too much overhead to write a separate Makefile to compile one C file, so how do you easily remember how to compile it? If there are no dependencies (i.e. library or include paths) then its pretty straightforward to run gcc or clang by hand. But what if there are a handful of dependent libraries that need to be linked to the program or include lines? Crafting that compile line can a frustrating exercise in searching your shell history. A possible solution is to embed the compile line embedded at the top of the C file, like so:

    /*
     * to compile: gcc hello.c -o hello -ldl -m -L/usr/local/lib -L/other/long path/my_lib/ -lmy_lib ...
     */
    ...
         
    int main() { ... }

But that's amature hour, here's how you really should do it ...

Read More