3

I am having a hard time figuring out how to add getopt.h in my AIX 7.1. I am using the getopt_long function in my code, which I know is in getopt.h instead of unistd.h (which contains getopt()).

This code is not compiling in AIX:

fatal error: getopt.h: No such file or directory
 #include <getopt.h>
                    ^
compilation terminated.

Here are the gcc packages installed:

gcc-4.8.3-1.aix7.1.ppc.rpm
gcc-c++-4.8.3-1.aix7.1.ppc.rpm
libgcc-4.8.3-1.aix7.1.ppc.rpm
gcc-cpp-4.8.3-1.aix7.1.ppc.rpm

I have gone through these links, but they haven't helped much:

Also a lot of other web searches.

Am I missing some rpms, or some environment variables?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Ravindra Mijar
  • 1,282
  • 2
  • 9
  • 17
  • I guess `getopt_long` and `getopt_long_only` are GNU-specific functions that might not exist in AIX. – Lorinczy Zsigmond Dec 02 '15 at 16:38
  • `getopt_long` is a GNU function. I don't know if it's any help, but on my Debian Linux machines, `dlocate` reports `libc6-dev:amd64: /usr/include/getopt.h` - i.e. it's part of **GNU libc**. In the worst case, you may have to resort to an `autoconf` test, and writing an `#ifdef` allowing only short options when it doesn't find `getopt_long()`. – Toby Speight Dec 11 '15 at 11:05

1 Answers1

0

getopt_long is GNU-only, but you can take free implementation (for example, this claims that it is of BSD license, if I understand it right) or write your own implementation (it's not so hard) and use it in your code.

vladon
  • 8,158
  • 2
  • 47
  • 91
  • 1
    AIX != GNU. So getopt_long is not native to it. A more authoritative source of a BSD licensed getopt_long is part of FreeBSD (and probably NetBSD) libc: https://svnweb.freebsd.org/base/head/lib/libc/stdlib/getopt_long.c?view=markup – Anders Dec 15 '15 at 03:56
  • @Anders "AIX != GNU", yes, I mean it under "`getopt_long` is GNU-only". – vladon Dec 15 '15 at 06:39
  • Thanks Vladon and Anders. I ended up coming across https://www.gnu.org/software/gnulib/manual/html_node/index.html#Top which allows GNU extensions to be pulled into non-GNU OSes. One has to download the source code, integrate it into your make / configure infrastructure, and then use it. The compilation of getopt.h worked by following the procedure mentioned in above link. However coz of some other compilation issues I haven't been able to test whether it actually works at runtime! – Ravindra Mijar Dec 17 '15 at 11:48
  • I ended up shifting to popt library instead. It is readily available as rpms on Linux and AIX. The code change wasn't much either. I only had to add extra parameters to the options variable to make it compatible to popt. The rpm for AIX is available on the perzl website - http://www.perzl.org/aix/index.php?n=Main.Popt – Ravindra Mijar Jan 21 '16 at 06:08