Ryan
Ryan

Reputation: 253

Compile Error with getaddrinfo

I am trying to learn the basics of socket programming and am using some code from a guide I found but I am getting compile errors. The code and errors are shown below

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#define PORT "21467"

int main(void)
{
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the results
memset(&hints, 0, sizeof hints); // make sure the struct is empty

hints.ai_family = AF_INET; 
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me

if ((status = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
    //fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
}
// servinfo now points to a linked list of 1 or more struct addrinfos
// ... do everything until you don't need servinfo anymore ....
freeaddrinfo(servinfo); // free the linked-list

return 0;
}

Compile errors:

server.c: In function 'main':
server.c:16: warning: incompatible implicit declaration of built-in function 'memset'
server.c:24: warning: incompatible implicit declaration of built-in function 'exit'
Undefined                       first referenced
 symbol                             in file
getaddrinfo                         /var/tmp//ccU0yRDe.o
freeaddrinfo                        /var/tmp//ccU0yRDe.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Any help would be greatly appreciated!

Upvotes: 1

Views: 1496

Answers (1)

Ed Heal
Ed Heal

Reputation: 59987

If you do a man memset, you will see that the manual page says #include <string.h> is required. Just put that at the top of the file. Do the same for exit

EDIT

Need -lnsl -lsocket for the linking (assuming linux)

Upvotes: 3

Related Questions