-2

I have the following problems while trying to implement the Singleton pattern in c++ :

  • external symbol not resolved __imp_WSAStartup referenced in the function "private: __cdecl MaWinsock::MaWinsock(void)" (??0MaWinsock@@AEAA@XZ) )
  • external symbol not resolved __imp_WSACleanup referenced in the function"public: __cdecl MaWinsock::~MaWinsock(void)" (??1MaWinsock@@QEAA@XZ)

Here is MaWinsock.h

    #include <WinSock2.h>

    class MaWinsock
    {
        WSADATA wsaData;    //  structure contenant les données de la librairie winsock à initialiser. représente la DLL.
        MaWinsock(void);

        static MaWinsock *instanceUnique;
    public:
        static MaWinsock *getInstance();
        ~MaWinsock(void);
    };

Here is MaWinsock.cpp

    #include <iostream>
    #include "Exception.h"
    #include "MaWinsock.h"


    using namespace std;

    /* static */   MaWinsock* MaWinsock::instanceUnique = NULL;

    /* static */ MaWinsock* MaWinsock::getInstance()
    {
        if (!instanceUnique) instanceUnique = new MaWinsock;
        return instanceUnique;
    }


    MaWinsock::MaWinsock(void)
    {

        int r;

        r = WSAStartup(MAKEWORD(2, 0), &wsaData);       // MAKEWORD(2,0) sert à indiquer la version de la librairie à utiliser : 1 pour winsock et 2 pour winsock2

                                                        /* en cas de succès, wsaData a été initialisée et l'appel a renvoyé la valeur 0 */

        if (r) throw Exception("L'initialisation a échoué");

        cout << "initialisation winsock effectuée" << endl;
    }


    MaWinsock::~MaWinsock(void)
    {
        WSACleanup();
        cout << "libération des ressources de la winsock effectuée" << endl;
    }

What is very strange is that on another related project this code works well but here,i can't figure out where the problem is.

If one of you is familiar with this pattern, any help would be appreciated.

Thank you

Mxsky
  • 749
  • 9
  • 27
  • 2
    I'm guessing you aren't linking to the Winsock library (ws_32.lib), so these functions cannot be resolved. Can you make sure you are linking to this library? – ajshort Nov 14 '15 at 13:53
  • Yes, this was exactly that.I post the answer.This is specific to Visual c++. – Mxsky Nov 14 '15 at 14:13

1 Answers1

0

In order to make it work, you should add this line in your cpp file :

#pragma comment(lib, "wsock32.lib")
Mxsky
  • 749
  • 9
  • 27
  • I found the answer to my question thanks to some comments so i post it.Therefore if in the future other people have this same error, they will know how to fix it ... as simple as that. – Mxsky Nov 14 '15 at 14:12
  • People already know how to fix this because this question is a duplicate. Also, this problem is not related to singleton at all, so your question title will confuse people. You may argue with it, just explain where is your question vary from the duplicate so it can be reopened. – VP. Nov 14 '15 at 14:14