Compiler: undefined reference

Hi,

I've a socket example that I'm trying to compile, but I get the following error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Compiler: Default compiler
Building Makefile: "D:\cpp_workspace\Makefile.win"
Executing  make...
make.exe -f "D:\cpp_workspace\Makefile.win" all
g++.exe -D__DEBUG__ Translator/socket.o Translator/socketClient.o Translator/main.o  -o "Translator.exe" -L"D:/Dev-Cpp/lib" -L"C:/WINNT/system32" -mwindows  -g3 

Translator/socket.o(.text+0x124):socket.cpp: undefined reference to `WSAStartup@8'
Translator/socket.o(.text+0x169):socket.cpp: undefined reference to `WSACleanup@0'
Translator/socket.o(.text+0x1aa):socket.cpp: undefined reference to `socket@12'
Translator/socket.o(.text+0x23e):socket.cpp: undefined reference to `socket@12'

Translator/socket.o(.text+0x4dc):socket.cpp: undefined reference to `closesocket@4'
Translator/socket.o(.text+0x56f):socket.cpp: undefined reference to `ioctlsocket@12'
Translator/socket.o(.text+0x5d3):socket.cpp: undefined reference to `recv@16'
Translator/socket.o(.text+0x784):socket.cpp: undefined reference to `recv@16'
Translator/socket.o(.text+0x941):socket.cpp: undefined reference to `send@16'
Translator/socket.o(.text+0x989):socket.cpp: undefined reference to `send@16'
Translator/socketClient.o(.text+0x7b):socketClient.cpp: undefined reference to `gethostbyname@4'
Translator/socketClient.o(.text+0x10a):socketClient.cpp: undefined reference to `htons@4'
Translator/socketClient.o(.text+0x159):socketClient.cpp: undefined reference to `connect@12'
Translator/socketClient.o(.text+0x169):socketClient.cpp: undefined reference to `WSAGetLastError@0'
Translator/socketClient.o(.text+0x2f9):socketClient.cpp: undefined reference to `gethostbyname@4' 


Here is the code:

socketClient.h
1
2
3
4
5
6
7
#include "socket.h"

class SocketClient : public Socket {
	  public:
  SocketClient(const std::string& host, int port);
};



socketClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "socket.h"
#include "socketClient.h"

SocketClient::SocketClient(const std::string& host, int port) : Socket() {
  std::string error;

  hostent *he;
  if ((he = gethostbyname(host.c_str())) == 0) {
    error = strerror(errno);
    throw error;
  }

  sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr = *((in_addr *)he->h_addr);
  memset(&(addr.sin_zero), 0, 8); 

  if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr))) {
    error = strerror(WSAGetLastError());
    throw error;
  }
}


socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef SOCKET_H
#define SOCKET_H

#include <winsock2.h>

#include <string>

enum TypeSocket {BlockingSocket, NonBlockingSocket};

class Socket {
public:

  virtual ~Socket();
  Socket(const Socket&);
  Socket& operator=(Socket&);

  std::string receiveLine();
  std::string receiveBytes();

  void close();

  // The parameter of SendLine is not a const reference
  // because SendLine modifes the std::string passed.
  void sendLine (std::string);

  // The parameter of SendBytes is a const reference
  // because SendBytes does not modify the std::string passed 
  // (in contrast to SendLine).
  void sendBytes(const std::string&);

protected:
/*
  friend class socketServer;
  friend class socketSelect;
*/

  Socket(SOCKET s);
  Socket();


  SOCKET s_;

  int* refCounter_;

private:
  static void start();
  static void end();
  static int  nofSockets_;
};

/*
class SocketClient : public Socket {
public:
  SocketClient(const std::string& host, int port);
};
*/

/*
class SocketServer : public Socket {
public:
  SocketServer(int port, int connections, TypeSocket type=BlockingSocket);

  Socket* Accept();
};
*/
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/wsapiref_2tiq.asp
/*
class SocketSelect {
  public:
    SocketSelect(Socket const * const s1, Socket const * const s2=NULL, TypeSocket type=BlockingSocket);

    bool Readable(Socket const * const s);

  private:
    fd_set fds_;
};
*/
#endif




socket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "socket.h"
#include <iostream>

int Socket::nofSockets_= 0;

void Socket::start() {
  if (!nofSockets_) {
    WSADATA info;
    if (WSAStartup(MAKEWORD(2,0), &info)) {
      throw "Could not start WSA";
    }
  }
  ++nofSockets_;
}

void Socket::end() {
  WSACleanup();
}

Socket::Socket() : s_(0) {
  start();
  // UDP: use SOCK_DGRAM instead of SOCK_STREAM
  s_ = socket(AF_INET,SOCK_STREAM,0);

  if (s_ == INVALID_SOCKET) {
    throw "INVALID_SOCKET";
  }

  refCounter_ = new int(1);
}

Socket::Socket(SOCKET s) : s_(s) {
  start();
  refCounter_ = new int(1);
};

// Clear socket
Socket::~Socket() {
  if (! --(*refCounter_)) {
    close();
    delete refCounter_;
  }

  --nofSockets_;
  if (!nofSockets_) end();
}

// Create socket
Socket::Socket(const Socket& o) {
  refCounter_=o.refCounter_;
  (*refCounter_)++;
  s_         =o.s_;

  nofSockets_++;
}

// Set socket
Socket& Socket::operator=(Socket& o) {
  (*o.refCounter_)++;

  refCounter_=o.refCounter_;
  s_         =o.s_;

  nofSockets_++;

  return *this;
}

// Close connection
void Socket::close() {
  closesocket(s_);
}

// Get bytes
std::string Socket::receiveBytes() {
  std::string ret;
  char buf[1024];
 
  while (1) {
    u_long arg = 0;
    if (ioctlsocket(s_, FIONREAD, &arg) != 0)
      break;

    if (arg == 0)
      break;

    if (arg > 1024) arg = 1024;

    int rv = recv (s_, buf, arg, 0);
    if (rv <= 0) break;

    std::string t;

    t.assign (buf, rv);
    ret += t;
  }
 
  return ret;
}

// Get a string line
std::string Socket::receiveLine() {
  std::string ret;
  while (1) {
    char r;

    switch(recv(s_, &r, 1, 0)) {
      case 0: // not connected anymore;
              // ... but last line sent
              // might not end in \n,
              // so return ret anyway.
        return ret;
      case -1:
        return "";
    }

    ret += r;
    if (r == '\n')  return ret;
  }
}

// Send line
void Socket::sendLine(std::string s) {
  s += '\n';
  send(s_,s.c_str(),s.length(),0);
}

// Send bytes
void Socket::sendBytes(const std::string& s) {
  send(s_,s.c_str(),s.length(),0);
}



main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "socket.h"
#include "socketClient.h"
#include <iostream>

using namespace std;

int main() {

  try {
    SocketClient s("www.google.com", 80);
	Socket* sk;
	sk = dynamic_cast<Socket*>(&s);
	cout << typeid(sk).name() << endl;

/*	
   ((Socket*) &s).sendLine("GET / HTTP/1.0");
    s.sendLine("Host: www.google.com");
    s.sendLine("");

    while (1) {
      string l = s.receiveLine();
      if (l.empty()) break;
      cout << l;
      cout.flush();
    }
*/
  } 
  catch (const char* s) {
    cerr << s << endl;
  } 
  catch (std::string s) {
    cerr << s << endl;
  } 
  catch (...) {
    cerr << "unhandled exception\n";
  }

  return 0;
}



I think that the problem it's because the compiler can't find:
 
#include <winsock2.h> 


but I explicit make a referece to the directory C:/WINNT/system32 where it exists the Ws2_32.dll file.

What's wrong?

Thanks,
PSC
Probably your compiler can't find the winsock library. If you're using gcc then add -lwsock32 command option
Topic archived. No new replies allowed.