1

I'm trying to build a specific program from the Netpbm library, specifically pamenlarge. When running make pamenlarge, I get the following error:

pmfileio.c:216:22: error: implicitly declaring library function 'strdup' with type 'char *(const char *)' [-Werror,-Wimplicit-function-declaration]
    filenameBuffer = strdup(filenameTemplate);
                     ^
pmfileio.c:216:22: note: include the header <string.h> or explicitly provide a declaration for 'strdup'

I checked pmfileio.c and <string.h> is included. Alternatively, I tried explicitely defining the function strdup() from open source material, but the same error message popped up.

Here are the includes in pmfileio.c:


#define _SVID_SOURCE
    /* Make sure P_tmpdir is defined in GNU libc 2.0.7 (_XOPEN_SOURCE 500
       does it in other libc's).  pm_config.h defines TMPDIR as P_tmpdir
       in some environments.
    */
#define _BSD_SOURCE    /* Make sure strdup is defined */
#define _XOPEN_SOURCE 500    /* Make sure ftello, fseeko, strdup are defined */
#define _LARGEFILE_SOURCE 1  /* Make sure ftello, fseeko are defined */
#define _LARGEFILE64_SOURCE 1 
#define _FILE_OFFSET_BITS 64
    /* This means ftello() is really ftello64() and returns a 64 bit file
       position.  Unless the C library doesn't have ftello64(), in which 
       case ftello() is still just ftello().

       Likewise for all the other C library file functions.

       And off_t and fpos_t are 64 bit types instead of 32.  Consequently,
       pm_filepos_t might be 64 bits instead of 32.
    */
#define _LARGE_FILES  
    /* This does for AIX what _FILE_OFFSET_BITS=64 does for GNU */
#define _LARGE_FILE_API
    /* This makes the the x64() functions available on AIX */

#include "netpbm/pm_config.h"
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#if HAVE_IO_H
  #include <io.h>  /* For mktemp */
#endif

#include "netpbm/pm_c_util.h"
#include "netpbm/mallocvar.h"
#include "netpbm/nstring.h"

#include "pm.h"

Here's the actual function where strdup is called:

static void
makeTmpfileWithTemplate(const char *  const filenameTemplate,
                        int *         const fdP,
                        const char ** const filenameP,
                        const char ** const errorP) {
    
    char * filenameBuffer;  /* malloc'ed */

    filenameBuffer = strdup(filenameTemplate); // The error is on this line here

    if (filenameBuffer == NULL)
        pm_asprintf(errorP, "Unable to allocate storage for temporary "
                    "file name");
    else {
        int rc;
        
        rc = mkstemp2(filenameBuffer);
        
        if (rc < 0)
            pm_asprintf(errorP,
                        "Unable to create temporary file according to name "
                        "pattern '%s'.  mkstemp() failed with errno %d (%s)",
                        filenameTemplate, errno, strerror(errno));
        else {
            *fdP = rc;
            *filenameP = filenameBuffer;
            *errorP = NULL;
        }
        if (*errorP)
            pm_strfree(filenameBuffer);
    }
}

Thank you in advance, I've been stuck trying to build this and don't really know where to go from here.

Update: I believe this project is running on C99, where a number of these functions are not available. How do I update this so I have access to these functions?

Nick D
  • 11
  • 2
  • 2
    `strdup` is not part of the C standard until C23. You might have to [specifically ask for it](https://en.cppreference.com/w/c/experimental/dynamic/strdup) as an extension. – BoP May 24 '23 at 17:22
  • Or simply write your own. You would a dozen implementations here on SO. – Harith May 24 '23 at 18:04
  • 1
    What OS are you using? – Shawn May 24 '23 at 18:13
  • @BoP the code op included *is* asking for the function to be defined (at least on a Linux/glibc environment) – Shawn May 24 '23 at 18:14
  • @Shawn using MacOS – Nick D May 24 '23 at 20:13
  • Insert this line into `pmfileio.c` as the very first line: `extern char *strdup(const char *);` – Lorinczy Zsigmond May 25 '23 at 10:09
  • If the problem is building it, you should at least state in your question which OS and compiler you are using, what source code you are trying to build (inc. version) and what commands you are using to build it. – Mark Setchell Jun 10 '23 at 18:18

0 Answers0