Link error: undefined reference to `htonl@4' with MinGW

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

Link error: undefined reference to `htonl@4' with MinGW

Markus Selve
I tried  to compile the following simple test program:

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

int main(void) {

  printf("A Big Endian 8 on intel: %lx\n", htonl(0x08));

  return 0;

}



selvem@B5561X1D ~/test
$ gcc -Wall -c conv.c

selvem@B5561X1D ~/test
$ gcc -Wall -o conv conv.o
conv.o(.text+0x47):conv.c: undefined reference to `htonl@4'


Ok, so maybe it didn't link the ws2_32 library, so I added that explicitly:

selvem@B5561X1D ~/test
$ gcc -Wall -lws2_32  -o conv conv.o
conv.o(.text+0x47):conv.c: undefined reference to `htonl@4'

No, the same error. So I wanted to check if the library contains that
procedure at all:

selvem@B5561X1D ~/test
$ strings /mingw/lib/libws2_32.a | grep htonl
_htonl@4
__imp__htonl@4
htonl
_htonl@4
__imp__htonl@4


At least it contains strings like that, even if they start with an
underscore. Some Compilers do add underscore before they search the
library. As an example: "printf" is in the libraries also with a leading
underscore.

So has anyone any hints what the problem is here?

Markus.



-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
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
|

Re: Link error: undefined reference to `htonl@4' with MinGW

Brian Dessent
Markus Selve wrote:

> selvem@B5561X1D ~/test
> $ gcc -Wall -lws2_32  -o conv conv.o
> conv.o(.text+0x47):conv.c: undefined reference to `htonl@4'

Congratulations, you just made probably the two most common mingw
errors.  If it makes you feel any better the archives of this list are
bulging at the seams with people making these mistakes over and over.

First, the order you specify things on the gcc command line matters.  If
A depends on B then B must come after A.  So put -lws2_32 after conv.o.

Second, you did not call WSAStartup() which is required before using ANY
socket function.  For something like htonl() it probably doesn't matter,
but for any program that actually does anything with sockets it will not
work.  <http://www.mingw.org/MinGWiki/index.php/sockets>

Brian


-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
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
|

Re: Link error: undefined reference to `htonl@4' with MinGW

rogerwells
In reply to this post by Markus Selve

gcc -ohtonl htonl.c -lwsock32

Roger Wells, P.E.
SAIC
221 Third St
Newport, RI 02840
401-847-4210 (voice)
401-849-1585 (fax)
[hidden email]



Markus Selve <[hidden email]>
Sent by: [hidden email]

07/14/2005 10:02 AM
Please respond to mingw-users

       
        To:        [hidden email]
        cc:        
        Subject:        [Mingw-users] Link error: undefined reference to `htonl@4' with MinGW



I tried  to compile the following simple test program:

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

int main(void) {

 printf("A Big Endian 8 on intel: %lx\n", htonl(0x08));

 return 0;

}



selvem@B5561X1D ~/test
$ gcc -Wall -c conv.c

selvem@B5561X1D ~/test
$ gcc -Wall -o conv conv.o
conv.o(.text+0x47):conv.c: undefined reference to `htonl@4'


Ok, so maybe it didn't link the ws2_32 library, so I added that explicitly:

selvem@B5561X1D ~/test
$ gcc -Wall -lws2_32  -o conv conv.o
conv.o(.text+0x47):conv.c: undefined reference to `htonl@4'

No, the same error. So I wanted to check if the library contains that
procedure at all:

selvem@B5561X1D ~/test
$ strings /mingw/lib/libws2_32.a | grep htonl
_htonl@4
__imp__htonl@4
htonl
_htonl@4
__imp__htonl@4


At least it contains strings like that, even if they start with an
underscore. Some Compilers do add underscore before they search the
library. As an example: "printf" is in the libraries also with a leading
underscore.

So has anyone any hints what the problem is here?

Markus.



-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
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
|

Re: Re: Link error: undefined reference to `htonl@4' with MinGW

Markus Selve
In reply to this post by Markus Selve
> Date: Thu, 14 Jul 2005 07:21:52 -0700
> From: Brian Dessent <[hidden email]>
> Organization: My own little world...
> To: [hidden email]
> Subject: Re: [Mingw-users] Link error: undefined reference to `htonl@4'
with

>  MinGW
> Reply-To: [hidden email]
>
> Markus Selve wrote:
>
> > selvem@B5561X1D ~/test
> > $ gcc -Wall -lws2_32  -o conv conv.o
> > conv.o(.text+0x47):conv.c: undefined reference to `htonl@4'
>
> Congratulations, you just made probably the two most common mingw
> errors.  If it makes you feel any better the archives of this list are
> bulging at the seams with people making these mistakes over and over.
>
> First, the order you specify things on the gcc command line matters.  If
> A depends on B then B must come after A.  So put -lws2_32 after conv.o.

Thanks. It works that way. I didn't know that the positon of an option
matters for gcc.

>
> Second, you did not call WSAStartup() which is required before using ANY
> socket function.  For something like htonl() it probably doesn't matter,
> but for any program that actually does anything with sockets it will not
> work.  <http://www.mingw.org/MinGWiki/index.php/sockets>

I really want only to use htonl(), no real operations with socket. I tried
it without WSAStartup() and it works.

Btw, I searched the mailing list before posing this question, but only
found a thread from the year 2002 saying, that, if the reference is
undefined, a rework of the source code for WIN32 was missing. Which
obviously was not the answer, but your's was: order matters.

So thanks again,

Markus.

>
> Brian
>
>
> --__--__--
>



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
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
|

Re: Link error: undefined reference to `htonl@4' withMinGW

Brian Dessent
Markus Selve wrote:

> I really want only to use htonl(), no real operations with socket. I tried
> it without WSAStartup() and it works.

Even if you only use htonl() I still recommend that you call
WSAStartup().  If it works despite this you are simply lucky and there
is no guarantee that it will continue to work with future versions of
Windows.  Depending on undefined behavior is not a good practice in any
programming environment.  MSDN says explicitly that you must do this:

The WSAStartup function must be the first Windows Sockets function
called by an application or DLL. It allows an application or DLL to
specify the version of Windows Sockets required and retrieve details of
the specific Windows Sockets implementation. The application or DLL can
only issue further Windows Sockets functions after successfully calling
WSAStartup.

<http://msdn.microsoft.com/library/en-us/winsock/winsock/wsastartup_2.asp>

Brian


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
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
|

Re: Re: Link error: undefined reference to `htonl@4' withMinGW

Markus Selve
> Markus Selve wrote:
>
> > I really want only to use htonl(), no real operations with socket. I
tried

> > it without WSAStartup() and it works.
>
> Even if you only use htonl() I still recommend that you call
> WSAStartup().  If it works despite this you are simply lucky and there
> is no guarantee that it will continue to work with future versions of
> Windows.  Depending on undefined behavior is not a good practice in any
> programming environment.  MSDN says explicitly that you must do this:
>
> The WSAStartup function must be the first Windows Sockets function
> called by an application or DLL. It allows an application or DLL to
> specify the version of Windows Sockets required and retrieve details of
> the specific Windows Sockets implementation. The application or DLL can
> only issue further Windows Sockets functions after successfully calling
> WSAStartup.
>
> <http://msdn.microsoft.com/library/en-us/winsock/winsock/wsastartup_2.asp
>

Ok, then I'll call WSAStartup() first.

Thanks for the advice,

Markus.
>
> Brian
>
>
> --__--__--
>



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

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