Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
gcc -ggdb -c pcs.c
gcc -ggdb -o pcs pcs.o -lm
Undefined first referenced
symbol in file
recv pcs.o
send pcs.o
gethostbyname pcs.o
socket pcs.o
connect pcs.o
ld: fatal: Symbol referencing errors. No output written to pcs
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `pcs'
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>#define PORT 0x1234
#define HOST "myHost"
#define DIRSIZE 8192int main(int argc, char *argv[])
{
char hostname[100];
char dir[DIRSIZE];
int sd;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent *hp;strcpy(hostname,HOST);
if (argc>2)
{ strcpy(hostname,argv[2]); }/* go find out about the desired host machine */
hp = gethostbyname(hostname);
/*if ((hp = gethostbyname(hostname)) == 0) {
perror("gethostbyname");
exit(1);
}*//* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(PORT);/* grab an Internet domain socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}/* connect to PORT on HOST */
connect(sd,(struct sockaddr *) &pin, sizeof(pin));
/* if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
perror("connect");
exit(1);
}*//* send a message to the server PORT on machine HOST */
send(sd, argv[1], strlen(argv[1]), 0);
/*if (send(sd, argv[1], strlen(argv[1]), 0) == -1) {
perror("send");
exit(1);
}*//* wait for a message to come back from the server */
recv(sd, dir, DIRSIZE, 0);
/*if (recv(sd, dir, DIRSIZE, 0) == -1) {
perror("recv");
exit(1);
}*/
printf("%s\n", dir);close(sd);
return 0;
}

That error means it can't find the recv() and the send() functions. Depending on your platform, it may be as simple as including a link to libsocket.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |