views:

174

answers:

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

+2  A: 

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
Yup, put an stdlib include at the top.
Aviral Dasgupta
No, including stdlib doesn't make it go away
Ralph
If that doesn't work, something is wrong.
Chris Lutz
I know, now all I need to do is figure out what it is :)Is there any other information I could provide which would be helpful?
Ralph
in glibc, realpath() is an extension.
Tim Post
+4  A: 

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
Thank you! I have much to learn about programming still!
Ralph
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
@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