getopt() and C99
Trying to compile ed2c0d5e results in the following error:
cc -o pdf -std=c99 -Wall -Werror -Wshadow -DLOG -I. -L./lib pdf.c lzw.c -lhammer -lz
pdf.c: In function ‘main’:
pdf.c:5498:15: error: implicit declaration of function ‘getopt’ [-Werror=implicit-function-declaration]
5498 | while ((ch = getopt(argc, argv, "L:qsS:vx:X:")) != -1) {
| ^~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:23: pdf] Error 1
although unistd.h
is included, which is supposed to declare it.
I believe this is due to the interaction of -std=c99
and GNU extensions. With -std=c99
, unistd.h
does not include getopt.h
, resulting in the above error. I committed a fix for this using glibc feature macros.
When researching the issue, I've seen multiple approaches suggested for this:
- Use
-std=gnu99
instead of-std=c99
for the GNU extensions - Set
_POSIX_C_SOURCE >= 2
in the Makefile- This is what I committed to master as a quick fix: 0e4e49e8
- Set
_POSIX_C_SOURCE >= 2
in the source
Which one of these is the better solution on the long run? Switching to -std=gnu99
, or setting the feature test macro?