|
From: <Dou...@ao...> - 2001-03-18 16:08:06
|
I am hoping someone who uses winsock can help me. I get the following linker
errors when I build the textbook example program shown below the error
listing:
c:\windows\TEMP\ccdc2agb.o: In function `doit(int, char **)':
//c/dev-c_~1/localh~1.cpp:7: undefined reference to `gethostname@8'
//c/dev-c_~1/localh~1.cpp:9: undefined reference to `WSAGetLastError@0'
//c/dev-c_~1/localh~1.cpp:14: undefined reference to `gethostbyname@4'
//c/dev-c_~1/localh~1.cpp:23: undefined reference to `inet_ntoa@4'
c:\windows\TEMP\ccdc2agb.o: In function `main':
//c/dev-c_~1/localh~1.cpp:32: undefined reference to `WSAStartup@8'
//c/dev-c_~1/localh~1.cpp:38: undefined reference to `WSACleanup@0'
#include <iostream.h>
#include <winsock.h>
int doit(int, char **)
{
char ac[80];
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
cerr << "Error " << WSAGetLastError() <<
" when getting local host name." << endl;
return 1;
}
cout << "Host name is " << ac << "." << endl;
struct hostent *phe = gethostbyname(ac);
if (phe == 0) {
cerr << "Yow! Bad host lookup." << endl;
return 1;
}
for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
}
return 0;
}
int main(int argc, char *argv[])
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
return 255;
}
int retval = doit(argc, argv);
WSACleanup();
return retval;
}
When I substitute "winsock2.h" for "winsock.h" I get the compiler error:
... \INCLUDE\winsock2.h:46:unbalanced '#endif'
|