8000 `make msieve` fails with GCC 14+: implicit declaration of `strdup` in demo.c (util.h pins `_POSIX_C_SOURCE` to 200112L) · Issue #102 · bbuhrow/yafu · GitHub
[go: up one dir, main page]

Skip to content

make msieve fails with GCC 14+: implicit declaration of strdup in demo.c (util.h pins _POSIX_C_SOURCE to 200112L) #102

Open

Description

@teruteru128

make msieve does not build with GCC 14 or later. This is unrelated to GPU support; I ran into it while setting up a fresh checkout.

Reproduce

A clean checkout of master (8110dfb), GMP and GMP-ECM installed, GCC 15.2.0 on Ubuntu 26.04:

make msieve
demo.c:392:73: error: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
  392 |   savefile_name = strdup(argv[i+1]);
make: *** [Makefile:1210: msieve] Error 1

Cause

demo.c uses strdup() but only includes <ms_msieve.h> and <signal.h>. <string.h> does get included, indirectly (ms_msieve.h → ms_include/util.h), but util.h does this first (line 28, non-Windows branch):

#define _POSIX_C_SOURCE 200112L

strdup is POSIX.1-2008, so with _POSIX_C_SOURCE pinned to the 2001 level glibc's <string.h> does not declare it. GCC up to 13 only warns about the implicit declaration; GCC 14 made it an error. (Same file, same flags: GCC 13 prints a warning, GCC 15 an error.)

Two possible fixes (I checked both: make msieve then completes with GCC 15.2.0)

  • A. Add #include <string.h> at the top of demo.c. One line; it works because <string.h> is then seen before util.h sets the macro (GCC prints a harmless "_POSIX_C_SOURCE redefined" warning).
  • B. Change util.h to #define _POSIX_C_SOURCE 200809L. Also one line, and it fixes every file that gets <string.h> through util.h, not just demo.c.

Why it probably went unnoticed

The CI runs on ubuntu-22.04 (GCC 11, where this is only a warning) and only builds make yafu, which never compiles demo.c. Building with -Werror=implicit-function-declaration would catch this even with an old compiler, and make msieve could be added to the workflow.

I'm happy to send a PR for A, B, or both, whichever you prefer. This is independent of #99/#100.

Disclosure: I worked this out with the help of an AI coding assistant (Claude Code); the build and checks above were run on my machine.

Activity

  1. teruteru128 commented on Oct 9, 2026

    @teruteru128
    ContributorAuthor

    Follow-up: the same macro hides a second function (nice), and this changes what I suggested above.

    nice() is hidden in the same way. common/util.c and top/driver.c call nice(100); with GCC 13 both give implicit declaration of function ‘nice’. cfe7255 already worked around this for GCC 14+ by compiling the call out (#if __GNUC__ < 14), so on GCC 14+ set_idle_priority() / yafu_set_idle_priority() no longer lower the priority at all.

    Correction to fix B above. nice() is only declared under _XOPEN_SOURCE or _DEFAULT_SOURCE (see man 2 nice, feature test macro requirements), so _POSIX_C_SOURCE 200809L fixes strdup but does not declare nice. What I tested (-std=gnu11 -Werror=implicit-function-declaration; GCC 13.4 and 15.2 give the same results):

    feature test macro strdup nice
    none (gnu11 default) declared declared
    _POSIX_C_SOURCE 200112L (what util.h, util.c and driver.c define now) not declared not declared
    _POSIX_C_SOURCE 200809L declared not declared
    _XOPEN_SOURCE 500, 600 or 700 declared declared
    _POSIX_C_SOURCE 200112L + _DEFAULT_SOURCE declared declared

    So a better one-line fix is to define _XOPEN_SOURCE 700 instead of _POSIX_C_SOURCE 200112L. I tried it on the real files (GCC 13): changing ms_include/util.h makes the nice warning in common/util.c go away; top/driver.c has its own #define _POSIX_C_SOURCE 200112L (line 21), so it needs the same change. Once nice is declared, the existing nice(100); produces a harmless -Wunused-result warning.

    Caveats: I only tested glibc with GCC 13 and 15. I can't say how _XOPEN_SOURCE behaves on macOS/BSD, where defining it can hide non-POSIX declarations. If you go this way, the #if __GNUC__ < 14 guards could probably be dropped so that the idle priority works on GCC 14+ again (I did not test that end to end).

    Happy to prepare a PR for whichever direction you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0