Undefined references to `ntohl@4' `htonl@4'

I'm using an open Huffman encoding library, implemented on C ( http://huffman.sourceforge.net ). However, I'm having the following linker errors testing it on windows (And compiling in c++).

[Linker error] undefined reference to `ntohl@4'
[Linker error] undefined reference to `htonl@4'


What is going on here? Did I forget to include something?
ntohl is networktohostlong function - it converts a long from bigEndian to littleEndian format.
It is part of network programming library. In windows the function prototype is in winsock2.h header file.
htonl is it's sister function and goes the oher way (littleEndian to BigEndian).
You need to link with the ws2_32.lib library.

Edit:
I think I better be more correct.
The network side is BigEndian.
on Intel processor systems, the host side is littleEndian.
On something like Motorola CPU the host will be bigEndian (and in this case ntohl and htonl would
effectively do nothing)

Last edited on

It is part of network programming library. In windows the function prototype is in winsock2.h header file.
htonl is it's sister function and goes the oher way (littleEndian to BigEndian).
You need to link with the ws2_32.lib library.

How do i do that??

I've tried #include <winsock2.h> ,but the compiler errors are still there (mainly because winsock2.h was already included in huffman.c). And i've got not a clue about how to link with the ws2_32.lib library (mainly because its not even included in dev cpp's files)
Last edited on
you just need to add a
 
-l ws2_32

as a compiler option in your IDE or your makefile or scratch or whatever you use to compile.
@JRevor

I wrote the huffman library. I'm not running windows, so I can't be sure, but maikel's solution sounds like what you need to do.

An "undefined reference" error means that the symbol ntohl@4 is not defined in any code you are linking with. As maikel noted, that symbol is in the ws2_32 library (the winsock 2 library). You need to tell your linker to that it needs to use the ws2_32 library while linking.

I'm not sure what compiler you're using (the thread at https://sourceforge.net/projects/huffman/forums/forum/307394/topic/3656554 only says you're on XP), but the -l option is something you'd give to gcc. If you're using a microsoft compiler, it may be another option.

The huffman.dsw (or maybe it's huffman.dsp, I don't remember) file included in the project is for Visual Studio 6.0. You could try opening one of those files and seeing if your newer microsoft IDE can open them. If so, you should be good to go. Another option is to get Cygwin and then just run make in the huffman directory. However, if you do that you won't be able to use the nice microsoft IDE debugger.
Last edited on
Topic archived. No new replies allowed.