Reputation: 11
I literally read every post on stackoverflow that is somewhat similar to my case and none worked. this is a snippet of IRC.cc after my modifications : (I am using library cpIRC if you want to see IRC.h its on google)
#include "IRC.h"
#ifdef __WIN32__
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
//#include <windows.h>
#pragma comment(lib,"ws2_32")
#else
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define closesocket(s) close(s)
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif
... code ... ERROR IS ON THIS LINE:
irc_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (irc_socket==INVALID_SOCKET)
{
return 1;
}
...code ...
ERRORS are as such :
||=== Build: Debug in botv (compiler: GNU GCC Compiler) ===|
obj\Debug\IRC.o||In function `ZN3IRC5startEPciS0_S0_S0_S0_':|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|107|undefined reference to `socket@12'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|112|undefined reference to `gethostbyname@4'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|115|undefined reference to `closesocket@4'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|120|undefined reference to `htons@4'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|122|undefined reference to `connect@12'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|125|undefined reference to `WSAGetLastError@0'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|127|undefined reference to `closesocket@4'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|137|undefined reference to `closesocket@4'|
obj\Debug\IRC.o||In function `ZN3IRC10disconnectEv':|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|163|undefined reference to `shutdown@8'|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|165|undefined reference to `closesocket@4'|
obj\Debug\IRC.o||In function `ZN3IRC12message_loopEv':|
C:\Users\aaa\Desktop\CPP\botv\IRC.cc|196|undefined reference to `recv@16'|
||=== Build failed: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I have read tons of answers about winsock2.h that is how I ended up with the includes you see and their order. it is still showing the same error. Any suggestions would be greatly appreciated
Upvotes: 1
Views: 4289
Reputation: 301
The errors are caused by a missing library. The missing library is ws2_32 and is found in the library file libws2_32.a (on Windows).
You actually attempt to link to this library with the line:
#pragma comment(lib,"ws2_32")
However, this is only recognized by the MSVC++ compiler while you are using the GCC compiler.
To solve the problem, remove the pragma-line (or leave it there, it probably doesn't matter), and add the library to the project. I think you add libraries in Code::Blocks by:
If the library is in the usual folder, this will eliminate your linker errors.
Upvotes: 2
Reputation: 310840
These are linker errors. You need to link to the appropriate Winsock .lib library.
Upvotes: -1