0

I am trying to implement a connection pool. Compiling works but ld is showing errors. Why the linker showing the error.

In Connecter.h

class Connecter
{
    private:    
        
        
        Connecter();
        ~Connecter();
        
        static MYSQL_POOL::ConnectionPool * pool;
    public:
        static sql::Connection *getCon();
        static void returnCon(sql::Connection *con);
    
};

In Connecter.cpp

Connecter::Connecter() {
    pool = new MYSQL_POOL::ConnectionPool("localhost", "root", "pass");
    pool->setSchema("webservice");
    pool->initialize(10);
}

Connecter::~Connecter() {
    pool->release();
}

sql::Connection * Connecter::getCon() {
    return pool->getConnection();
}

void Connecter::returnCon(sql::Connection *con) {
    pool->closeConnection(con);
}

I get am getting this error

/usr/bin/ld: /tmp/ccTlL8Sv.o: in function `Connecter::Connecter()':
Connecter.cpp:(.text+0xb8): undefined reference to `Connecter::pool'
/usr/bin/ld: Connecter.cpp:(.text+0xe6): undefined reference to `Connecter::pool'
/usr/bin/ld: Connecter.cpp:(.text+0x130): undefined reference to `Connecter::pool'
/usr/bin/ld: /tmp/ccTlL8Sv.o: in function `Connecter::~Connecter()':
Connecter.cpp:(.text+0x19b): undefined reference to `Connecter::pool'
/usr/bin/ld: /tmp/ccTlL8Sv.o: in function `Connecter::getCon()':
Connecter.cpp:(.text+0x1b7): undefined reference to `Connecter::pool'
/usr/bin/ld: /tmp/ccTlL8Sv.o:Connecter.cpp:(.text+0x1ca): more undefined references to `Connecter::pool' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:16: all] Error 1

I cant figure out what the error is. I have included the Connecter.h in Connecter.cpp. please help me

Ali mus
  • 13
  • 3
  • Mmm I wanted to answer, but it is already closed :/ TDLR: put `MYSQL_POOL::ConnectionPool* Connecter::pool = new MYSQL_POOL::ConnectionPool("localhost", "root", "pass");` in your source file; – pptaszni Jan 20 '21 at 18:38
  • 1
    @pptaszni You don't want to answer this question. It's been answered to death. Googling the the question title, for me at any rate, pops up the answer on several sites on the first page, including Stack Overflow. Expanding the hits on Stack Overflow alone, we get over 16000, the first page of them look to be good quality answers. Google is notorious for sending different results to people, but given the volume of results, I believe it will be hard to NOT find the answer with a relatively small amount of effort. That said, there are more exact duplicates, and I'll be adding one in a moment – user4581301 Jan 20 '21 at 18:47
  • @user4581301, yep, you are right – pptaszni Jan 20 '21 at 22:33

0 Answers0