Quantcast

Undefined reference to 'getaddrinfo'

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Undefined reference to 'getaddrinfo'

James Steward-2
Hi All,

I've googled for this for a while now and I'm not sure what to do next.

I have a small c program that calls getaddrinfo and freeaddrinfo.

I have the header file included for ws2tcpip.h.

When I compile with
gcc -Wall -o prog main.c -lws2_32

I get warnings about implicit declarations of the 2 functions and
undefined references to the 2 functions from the linker.

In the ws2tcpip.h I see
#if (_WIN32_WINNT >= 0x0501)

---declaration of getaddrinfo

#else
/* FIXME ...

When I print the value of _WIN32_WINNT I get 0x400 so obviously the
function isn't declared.  That explains the implicit declarations
warning.  But what about the undefined references?  The functions are in
the library ws2_32.a.

What do I need to do to resolve this?  Is it a MinGW version or a
Windows version that needs updating?  Or something completely different?

I've attached a smaller test case that behaves similarly.

Regards,
James.

/*
gcc -Wall -o test test.c -lws2_32
test.c: In function `main':
test.c:27: warning: implicit declaration of function `getaddrinfo'
C:/DOCUME~1/James/LOCALS~1/Temp/ccw5baaa.o(.text+0xeb):test.c: undefined
reference to `getaddrinfo'
collect2: ld returned 1 exit status
 */  

#include <windows.h>
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define DEF_PORT "27015"

int main(int argc, char **argv) {

        int rv;
        WSADATA wsaData;
        struct addrinfo *result = NULL, hints;

        rv = WSAStartup((short)0x0202, &wsaData);
       
        if (rv != 0) {
                printf("WSAStartup failed: %d\n", rv);
                return 1;
        }
       
        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_DGRAM;
        hints.ai_protocol = IPPROTO_UDP;
        hints.ai_flags = AI_PASSIVE;

        rv = getaddrinfo(NULL, DEF_PORT, &hints, &result);
        if (rv != 0) {
                printf("getaddrinfo failed: %d\n", rv);
                WSACleanup();
                return 1;
        }

        WSACleanup();

        return 0;
}




-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Undefined reference to 'getaddrinfo'

James Steward-2
On Fri, 2006-08-25 at 15:26 +1000, James Steward wrote:

> In the ws2tcpip.h I see
> #if (_WIN32_WINNT >= 0x0501)
>
> ---declaration of getaddrinfo
>
> #else
> /* FIXME ...

I think I've just answered my own question.

I added
#define _WIN32_WINNT  0x501
to the very top of the file and it seems to compile and run!

Was this the right thing to do?  I didn't see this documented in the
MSDN help - but that's not saying much :)

Cheers,
JS.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Undefined reference to 'getaddrinfo'

Michael Gerdau
In reply to this post by James Steward-2
> I have a small c program that calls getaddrinfo and freeaddrinfo.

>
> I have the header file included for ws2tcpip.h.
>
> When I compile with
> gcc -Wall -o prog main.c -lws2_32
>
> I get warnings about implicit declarations of the 2 functions and
> undefined references to the 2 functions from the linker.
>
> In the ws2tcpip.h I see
> #if (_WIN32_WINNT >= 0x0501)
>
> ---declaration of getaddrinfo
>
> #else
> /* FIXME ...
>
> When I print the value of _WIN32_WINNT I get 0x400 so obviously the
> function isn't declared.  That explains the implicit declarations
> warning.  But what about the undefined references?  The functions are in
> the library ws2_32.a.
>
> What do I need to do to resolve this?  Is it a MinGW version or a
> Windows version that needs updating?  Or something completely different?
The lib you link to by providing -lws2_32 is an importlib. Whether that
contains the required functions of not is pretty irrelevant.

The question is:
Does your OS provide them (you didn't tell us the OS you are using). Search
MSDN for these functions to check which OS versions support them, thereby
finding out whether your particular environment provides them.

If it doesn't you see why it is wise MinGW is conservative here.
If it does then define WINVER (not _WIN32_WINNT unless you know what
you are doing) to 0x0501 at the top of your program.

FYI this and a few other constants are defined in windef.h.

HTH, best,
Michael
--
 Vote against SPAM - see http://www.politik-digital.de/spam/
 Michael Gerdau       email: [hidden email]
 GPG-keys available on request or at public keyserver

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Undefined reference to 'getaddrinfo'

Brian Dessent
In reply to this post by James Steward-2
James Steward wrote:

> I've googled for this for a while now and I'm not sure what to do next.

Try searching the mailing list archives first.  This has come up before,
e.g. <http://sourceforge.net/mailarchive/message.php?msg_id=14887873>.

> When I print the value of _WIN32_WINNT I get 0x400 so obviously the
> function isn't declared.  That explains the implicit declarations
> warning.  But what about the undefined references?  The functions are in
> the library ws2_32.a.
>
> What do I need to do to resolve this?  Is it a MinGW version or a
> Windows version that needs updating?  Or something completely different?

The problem, as I described before, is that getaddrinfo() only exists in
ws2_32 for Windows XP and higher.  If you're willing to generate a
binary that only runs on XP (and fails with a "Cannot find procedure
entry point <whatever>" popup box on all other versions) then just do:

#define WINVER WindowsXP

...before including any w32api headers.  This signals that you want the
feature set of XP, and you don't care about older versions.  Note that
WindowsXP is defined as 0x0501, and that _WIN32_WINNT gets set to WINVER
if not already defined, so this is the proper way of declaring this, not
"#define _WIN32_WINNT 0x0501".

Now, to complicate things, Microsoft has added support for a
getaddrinfo() wrapper in their platform SDK, which works only for IPv4,
as a way of supporting such code on older versions of Windows.  So if
you are using the platform SDK you can still call getaddrinfo() from
older versions, but you'll really be just calling the existing
underlying IPv4 function (gethostbyname() or whatever) and failing if
you try to use IPv6.

So, as I said in that reply above, your options are essentially:

1. Live with XP-only binary.
2. Write (and potentially contribute to the w32api project) a suitable
getaddrinfo() wrapper for older versions.
3. If you're just using IPv4 then call the old standard IPv4 functions
directly (gethostby*).

Note that the reason that you got a link error when you did not define
WINVER is because the getaddrinfo function (and most every Windows API
function) is declared as type WINAPI which is synonymous with the
stdcall calling convention, which dictates a function decoration of @nn
on the end.  But if you call a function without a prototype (as happens
if you don't set WINVER high enough) then the compiler assumes the
default cdecl calling convention which does not have that decoration,
and thus you're trying to call an undecorated function, and the name
lookup fails at link time.  Another very common way that this plays out
is that the function actually only exists as FooA and FooW for the ANSI
and UNICODE variants, but you're meant to call it as just Foo() and the
include header #defines it to the correct variant based on whether you
enabled a Unicode build.  Here you get a similar link error if you call
a function that was not declared in a header.

Brian

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Undefined reference to 'getaddrinfo'

James Steward-2
In reply to this post by Michael Gerdau
On Fri, 2006-08-25 at 07:38 +0200, Michael Gerdau wrote:

> > I have a small c program that calls getaddrinfo and freeaddrinfo.
> >
> > I have the header file included for ws2tcpip.h.
> >
> > When I compile with
> > gcc -Wall -o prog main.c -lws2_32
> >
> > I get warnings about implicit declarations of the 2 functions and
> > undefined references to the 2 functions from the linker.
> >
> > In the ws2tcpip.h I see
> > #if (_WIN32_WINNT >= 0x0501)
> >
> > ---declaration of getaddrinfo
> >
> > #else
> > /* FIXME ...
> >
> > When I print the value of _WIN32_WINNT I get 0x400 so obviously the
> > function isn't declared.  That explains the implicit declarations
> > warning.  But what about the undefined references?  The functions are in
> > the library ws2_32.a.
> >
> > What do I need to do to resolve this?  Is it a MinGW version or a
> > Windows version that needs updating?  Or something completely different?
>
> The lib you link to by providing -lws2_32 is an importlib. Whether that
> contains the required functions of not is pretty irrelevant.
>
> The question is:
> Does your OS provide them (you didn't tell us the OS you are using). Search
> MSDN for these functions to check which OS versions support them, thereby
> finding out whether your particular environment provides them.

Ah ha.  XP Pro is what the app will need to run on.

> If it doesn't you see why it is wise MinGW is conservative here.
> If it does then define WINVER (not _WIN32_WINNT unless you know what
> you are doing) to 0x0501 at the top of your program.

> FYI this and a few other constants are defined in windef.h.

Thanks for your help.

Regards,
James.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Undefined reference to 'getaddrinfo'

James Steward-2
In reply to this post by Brian Dessent
On Thu, 2006-08-24 at 23:20 -0700, Brian Dessent wrote:

> James Steward wrote:
>
> > I've googled for this for a while now and I'm not sure what to do next.
>
> Try searching the mailing list archives first.  This has come up before,
> e.g. <http://sourceforge.net/mailarchive/message.php?msg_id=14887873>.
>
> > When I print the value of _WIN32_WINNT I get 0x400 so obviously the
> > function isn't declared.  That explains the implicit declarations
> > warning.  But what about the undefined references?  The functions are in
> > the library ws2_32.a.
> >
> > What do I need to do to resolve this?  Is it a MinGW version or a
> > Windows version that needs updating?  Or something completely different?
>
> The problem, as I described before, is that getaddrinfo() only exists in
> ws2_32 for Windows XP and higher.  If you're willing to generate a
> binary that only runs on XP (and fails with a "Cannot find procedure
> entry point <whatever>" popup box on all other versions) then just do:
>
> #define WINVER WindowsXP
>
> ...before including any w32api headers.  This signals that you want the
> feature set of XP, and you don't care about older versions.  Note that
> WindowsXP is defined as 0x0501, and that _WIN32_WINNT gets set to WINVER
> if not already defined, so this is the proper way of declaring this, not
> "#define _WIN32_WINNT 0x0501".
>
> Now, to complicate things, Microsoft has added support for a
> getaddrinfo() wrapper in their platform SDK, which works only for IPv4,
> as a way of supporting such code on older versions of Windows.  So if
> you are using the platform SDK you can still call getaddrinfo() from
> older versions, but you'll really be just calling the existing
> underlying IPv4 function (gethostbyname() or whatever) and failing if
> you try to use IPv6.
>
> So, as I said in that reply above, your options are essentially:
>
> 1. Live with XP-only binary.
> 2. Write (and potentially contribute to the w32api project) a suitable
> getaddrinfo() wrapper for older versions.
> 3. If you're just using IPv4 then call the old standard IPv4 functions
> directly (gethostby*).
>
> Note that the reason that you got a link error when you did not define
> WINVER is because the getaddrinfo function (and most every Windows API
> function) is declared as type WINAPI which is synonymous with the
> stdcall calling convention, which dictates a function decoration of @nn
> on the end.  But if you call a function without a prototype (as happens
> if you don't set WINVER high enough) then the compiler assumes the
> default cdecl calling convention which does not have that decoration,
> and thus you're trying to call an undecorated function, and the name
> lookup fails at link time.  Another very common way that this plays out
> is that the function actually only exists as FooA and FooW for the ANSI
> and UNICODE variants, but you're meant to call it as just Foo() and the
> include header #defines it to the correct variant based on whether you
> enabled a Unicode build.  Here you get a similar link error if you call
> a function that was not declared in a header.

What a twisted tail...but thanks Brian.  I'll see how I go from here.
An XP only binary might be ok too.  The target platform is XP Pro I
believe.

Cheers,
James.

P.S.  Now I know why I put my hand down when the boss says write some
program for Windows...


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Loading...