Thread: Porting Beej to Windows,

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    Porting Beej to Windows,

    I've mostly figured out how to port Beejs guide to windows.

    I'm trying out the "Gethostbyname" lookup program. And I keep getting linker errors saying,

    Undefined reference to "herror"

    My source code is as follows, and I'm linking to libwsock32.a
    I'm compiling in Dev-C++ 4.9.8.0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock.h>
    #include <errno.h>
    int main(int argc, char *argv[])
    {
        WSADATA wsaData; // if this doesn’t work
        //WSAData wsaData; // then try this instead
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) 
        {
            fprintf(stderr, "WSAStartup failed.\n");
            exit(1);
        }
    
    struct hostent *h;
        if (argc != 2) 
        { // error check the command line
            fprintf(stderr,"usage: getip address\n");
            exit(1);
        }
        if ((h=gethostbyname(argv[1])) == NULL) 
        { // get the host info
            herror("gethostbyname");
            exit(1);
        }
        WSACleanup();
    
        printf("Host name : %s\n", h->h_name);
        printf("IP Address : %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
    
    
    
        system("PAUSE");	
        return 0;
    }


    Thank you for the help
    Michael



    PS: If I comment out the line refering to herror, it compiles and runs fine....
    Last edited by m.mixon; 06-24-2006 at 04:37 PM. Reason: additional info

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Truthfully, never heard of herror(). All the man pages I can find say it's deprecated. Chances are there's not a Window's equivalent (though I could be wrong), but at any rate, it seems to be error handling. If it is, Winsock errors are different from Unix ones. You can (and perhaps should) substitute it with your own error function, perhaps a fprintf() to stderr.
    The herror() function writes a message to the diagnostic output consisting of the string parameter s, the constant string ": ", and a message corresponding to the value of h_errno.
    So, basically what you pass + ":" + a message depending on h_error, a global variable probably not there in Winsock.
    EDIT: Yeah, not there as far as I can tell. Use WSAGetLastError().

    Oh, and you don't call WSACleanup() on error.
    Last edited by Cactus_Hugger; 06-24-2006 at 08:57 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  2. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  3. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. porting c program from unix to windows
    By sunsail in forum Windows Programming
    Replies: 1
    Last Post: 10-19-2002, 06:55 AM