Thread: Having trouble compiling tutorial program :S

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Having trouble compiling tutorial program :S

    Hi, im trying to compile some code right out of beej's tutorial but at compile time it comes up with a whole array of errors i'ved linked libwsock32.a, included winsock.h and ran the WSAstartup code (also cut and pasted directly out of the book) like the tutoiral says too but it's got me stumped.

    Here are the errors im getting:
    Code:
    getaddrinfo undecleared first use of function
    gai_strerror undecleared first use of function
    inet_ntop undecleared first use of function
    freeaddrinfo undecleared first use of function
    invalid use of undefined type `struct sockaddr_in6'
    forward declaration of `struct sockaddr_in6'
    and heres the code:

    Code:
    /*
    ** showip.c -- show IP addresses for a host given on the command line
    */
    
    
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    //#include <sys/socket.h>
    //#include <netdb.h>
    //#include <arpa/inet.h>
    #include <winsock.h>
    
    struct addrinfo {
    int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
    int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
    int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
    int ai_protocol; // use 0 for "any"
    size_t ai_addrlen; // size of ai_addr in bytes
    struct sockaddr *ai_addr; // struct sockaddr_in or _in6
    char *ai_canonname; // full canonical hostname
    struct addrinfo *ai_next; // linked list, next node
    };
    
    
    int main(int argc, char *argv[])
    {
        WSADATA wsaData; // if this doesn't work
    //  WSAData wsaData; // then try this instead
    // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
    if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup failed.\n");
    exit(1);
    }
        
        
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];
    if (argc != 2) {
    fprintf(stderr,"usage: showip hostname\n");
    return 1;
    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;
    if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
    return 2;
    }
    printf("IP addresses for %s:\n\n", argv[1]);
    for(p = res;p != NULL; p = p->ai_next) {
    void *addr;
    char *ipver;
    // get the pointer to the address itself,
    // different fields in IPv4 and IPv6:
    if (p->ai_family == AF_INET) { // IPv4
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
    addr = &(ipv4->sin_addr);
    ipver = "IPv4";
    } else { // IPv6
    struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
    addr = &(ipv6->sin6_addr);
    ipver = "IPv6";
    }
    // convert the IP to a string and print it:
    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    printf(" %s: %s\n", ipver, ipstr);
    }
    freeaddrinfo(res); // free the linked list
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It's much easier to read if you post code with proper indentation.

    You should link to libws2_32.a instead of libwsock32.a.

    You should #include <winsock2.h> instead of winsock.h.

    Also, you should comment out #include <sys/types.h>.

    Beej's Guide to Network Programming was updated sometime last year and includes more IPv6 friendly functions which (I think) are only included in Winsock 2 on Windows. You might also want to initialise Winsock to 2.0 instead of 1.1 when you call WSAStartup, and also call WSACleanup once you are done.

    Also remember that on windows, close is closesocket (which I don't think you have used anyway, but it's good to know).

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Quote Originally Posted by DeadPlanet View Post
    It's much easier to read if you post code with proper indentation.
    Yea sorry about that I was in a bit of a rush so i didn't clean up the code.
    Last edited by Matty_Alan; 04-08-2010 at 12:17 AM.

  4. #4
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Ok iv'e tried linking the new winsock and changing the headers but it's still comming up with the same error(s),

    Errors:
    Code:
    'getaddrinfo' undeclared (first use this function) 
    'freeaddrinfo' undeclared (first use this function)
    here's a simpler version of the code that comes up with the same errors
    Code:
    #include <winsock2.h>
    
    struct addrinfo {
    int ai_flags;              // AI_PASSIVE, AI_CANONNAME, etc.
    int ai_family;             // AF_INET, AF_INET6, AF_UNSPEC
    int ai_socktype;           // SOCK_STREAM, SOCK_DGRAM
    int ai_protocol;           // use 0 for "any"
    size_t ai_addrlen;         // size of ai_addr in bytes
    struct sockaddr *ai_addr;  // struct sockaddr_in or _in6
    char *ai_canonname;        // full canonical hostname
    struct addrinfo *ai_next;  // linked list, next node
    };
    
    int main()
    {
        char a = 'b';
        int status;
    
        struct addrinfo hints, *res, *p;
    
        WSADATA wsaData;
        
        WSAStartup(MAKEWORD(2,0), &wsaData);
    
        status = getaddrinfo(a, NULL, &hints, &res);
        
        freeaddrinfo(res);
        
        return 0;
        
       }

  5. #5
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    oh... and This is how iv'e linked it:

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getaddrinfo Function (Windows)

    Scroll to the end of the page

    Find Requirements

    And look at the required header file to be included
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    If the way you linked doesn't work (I'm using Code::Blocks, I'd say it's the same but it may work) then use -lws2_32 as the linker argument instead.

    vart said it, Microsoft has fairly good documentation, please try to consult it first.

  8. #8
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Oh that worked fine i just needed to add the ws2tcpip header thanx for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-12-2009, 04:21 AM
  2. error compiling a little program
    By fbs777 in forum C Programming
    Replies: 4
    Last Post: 03-17-2009, 10:35 PM
  3. Compiling a program for Mac OSX from a windows computer
    By Jedi Nescioquis in forum Windows Programming
    Replies: 1
    Last Post: 02-13-2009, 03:06 PM
  4. Problem Compiling Program
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:56 PM
  5. Replies: 7
    Last Post: 12-14-2003, 01:00 PM