0

I was trying to define function that was declared in class (in header .h file). But there are still compiling problems with this separated definition. No matter if it is in these same file with declaration or in separated .cpp it still return these same error

||=== Build: Debug in Server (compiler: GNU GCC Compiler) ===|

obj\Debug\SocketManager.o||In function

`ZN13SocketManager12setUpWINSOCKEv':|

J:\CodeBlocksProjects\Server\SocketManager.h|18|undefined reference to

`WSAStartup@8'| ||error: ld returned 1 exit status| ||=== Build

failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

#include <iostream>
#include <winsock2.h>

using namespace std;

class SocketManager
{
private:
   WSADATA wsaData;
public:
   void setUpWINSOCK();
   SocketManager(int = 27015, string = "127.0.0.1");
};

void SocketManager::setUpWINSOCK()
{
int result = WSAStartup( MAKEWORD( 2, 2 ), & wsaData );
if( result != NO_ERROR )
   cout<<"Initialization error.\n"<<endl;
}

With small change its compilling propetly, but i would like to make it work with separated definition in diffrent .cpp file

#include <iostream>
#include <winsock2.h>

using namespace std;
class SocketManager
{
private:
   WSADATA wsaData;
public:
   void setUpWINSOCK()
   {
      int result = WSAStartup( MAKEWORD( 2, 2 ), & wsaData );
      if( result != NO_ERROR )
         cout<<"Initialization error.\n"<<endl;
   }
   SocketManager(int = 27015, string = "127.0.0.1");
};

Im aware that im probably messing up something very basic, but i have no idea how to even search for this. Im new to Cpp, and even if i know Java its not helping with this kind of errors. Also sorry for my english, im not used to using it in written form. I have never asked any question before on any other forum. Thats why im asking for patience :)

All help and links appreciated.

Community
  • 1
  • 1
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – 0x5453 Jun 23 '17 at 13:10
  • Where is `WSAStartup` coming from? Check your libraries... [WSAStartup](https://msdn.microsoft.com/en-us/library/windows/desktop/ms742213(v=vs.85).aspx) – DIEGO CARRASCAL Jun 23 '17 at 13:13
  • The layout of your code is just a red herring here - this is a linker error, not a compilation error. What does your linker string look like? How are you compiling this? – George Newton Jun 23 '17 at 13:14
  • You need to link with the MS Window's winsock lib see here for name: https://msdn.microsoft.com/en-us/library/windows/desktop/ms742213(v=vs.85).aspx – Richard Critten Jun 23 '17 at 13:15
  • Specifically, `ws2_32.lib` – WhozCraig Jun 23 '17 at 13:16

1 Answers1

0

WSAStartup@8 is a C++ mangled name. But the winsock library exports a C function WSAStartup. You have somewhere defined a prototype of WSAStartup before (or instead) including the "winsock.h" or "winsock2.h" header file.

Removing that wrong prototype will solve this linker problem.

Further make sure that you link with ws2_32.lib.

harper
  • 13,345
  • 8
  • 56
  • 105