Вот еще одна программа, которая вызвала у компилятора недопонимание.
echo-thread
Код: Выделить всё
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <pthread.h>
using namespace std;
void PANIC(char* msg);
#define PANIC(msg) { perror(msg); exit(-1); }
/*--- Child - echo servlet ---*/
void* Child(void* arg)
{ char line[100];
int bytes_read;
int client = *(int *)arg;
do
{
bytes_read = recv(client, line, sizeof(line), 0);
send(client, line, bytes_read, 0);
}
while (strncmp(line, "bye\r", 4) != 0);
close(client);
return arg;
}
/*--- main - setup server and await connections (no need to clean ---*/
/*--- up after terminated children. ---*/
int main(void)
{ int sd;
struct sockaddr_in addr;
if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
PANIC("Socket");
addr.sin_family = AF_INET;
addr.sin_port = htons(9999);
addr.sin_addr.s_addr = INADDR_ANY;
if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
PANIC("Bind");
if ( listen(sd, 20) != 0 )
PANIC("Listen");
while (1)
{ int client, addr_size = sizeof(addr);
pthread_t child;
client = accept(sd, (struct sockaddr*)&addr, (socklen_t*)&addr_size);
printf("Connected: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
if ( pthread_create(&child, NULL, Child, &client) != 0 )
perror("Thread creation");
else
pthread_detach(child); /* disassociate from parent */
}
return 0;
}
Компилятор пишет следующее:
Код: Выделить всё
undefined reference to `pthread_create'
undefined reference to `pthread_detach'
Подскажите, пожайлуста, где ошибка.