Search code examples
cgetoptgetopt-long

Bundling getopt_long() with my own code?


I have my own C project on github. I want to add long-option support by using GNU getopt_long() (which has it's own git repository). I believe there are 4 ways I can do this:

  1. Use a git submodule.
  2. Use a git subtree.
  3. Import the source of the current (2.22.6) snapshot into my own git repository.
  4. Import only the getopt_long() source files of getopt.c, getopt1.c, and gnugetopt.h.

The advantage of 1 and 2 is that I track getopt_long(). The advantages of 3 and 4 are that if the git repository of getopt_long() moves, it wont break my repository and it's much simpler to do.

Is there a recommended "GNU way" to do this?


Solution

  • After a bit more Googl'ing, I found the official GNU answer.

    Briefly, there are two recommended approaches:

    Use Gnulib (the "new" way)

    Using the gnulib-tool command, one imports getopt to compile it as a subset of the GNU library by making a few tweaks to one's configure.ac and Makefile.am files.

    Use getopt_long sources (the "old" way)

    Import the getopt_long sources of getopt.c, getopt1.c, and gnugetopt.h and use the adl_func_getopt_long.m4 macro to test for the existence of getopt_long on the platform and compile the imported sources only if necessary.