2

When I compile the following code:

#define _POSIX_C_SOURCE 200112L
#define _ISOC99_SOURCE
#define __EXTENSIONS__

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>    

int
main(int argc, char *argv[])
{
    char *symlinkpath = argv[1];
    char actualpath [PATH_MAX];
    char *ptr;
    ptr = realpath(symlinkpath, actualpath);
    printf("%s\n", ptr);
}

I get a warning on the line that contains the call to the realpath function, saying:

warning: assignment makes pointer from integer without a cast

Anybody know what's up? I'm running Ubuntu Linux 9.04

Ralph
  • 6,249
  • 7
  • 23
  • 19

2 Answers2

4

This is very simple. Glibc treats realpath() as a GNU extension, not POSIX. So, add this line:

#define _GNU_SOURCE

... prior to including stdlib.h so that it is prototyped and known to to return char *. Otherwise, gcc is going to assume it returns the default type of int. The prototype in stdlib.h is not seen unless _GNU_SOURCE is defined.

The following complies fine without warnings with -Wall passed:

#include <stdio.h>
#include <limits.h>

#define _GNU_SOURCE
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    char *symlinkpath = argv[1];
    char actualpath [PATH_MAX];
    char *ptr;
    ptr = realpath(symlinkpath, actualpath);
    printf("%s\n", ptr);

    return 0;
}

You will see similar behavior with other popular extensions such as asprintf(). Its worth a look at /usr/include/ to see exactly how much that macro turns on and what it changes.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
  • Actually you should read `man feature_test_macros`. Also, the manpages for `realpath`, `asprintf`, etc. all tell which macros must be present for their declaration. – ephemient Oct 18 '09 at 04:08
  • @ephemient: The headers are the documentation when it comes to altering the behavior of the standard C library. Call me paranoid, but errata happens in documentation updates from release to release. Sometimes things are incorrect, sometimes changes don't creep into documentation .. sometimes new changes are documented incorrectly. I know studying glibc has been known to make heads explode, but its worth a look at the headers. You need to know what each macro actually _does_ , which is best obtained via grep and other means. – Tim Post Oct 29 '09 at 10:47
2

The compiler doesn't know what realpath is, so it assumes it's a function returning int. It does this for historical reasons: a lot of older C programs relied on it doing this.

You're probably missing the declaration of it, e.g. by forgetting to #include its header file.

Edmund
  • 10,533
  • 3
  • 39
  • 57