Link Errors Compiling MySQL C++ Programs on 64-bit Windows
I’m starting to code up a simple MySql connector class. Here is the code for the class. First the header:
#ifndef DBC_H #define DBC_H #include "kx.h" #include "my_global.h" #include "mysql.h" class DBConnector { public: DBConnector( const char* server, const char* user, const char* password, const char* database ) : mServer( server ), mUser( user ), mPassword( password ), mDatabase( database ), mConn( NULL ) {} DBConnector( KxSymbol server, KxSymbol user, KxSymbol password, KxSymbol database ) : mServer( server ), mUser( user ), mPassword( password ), mDatabase( database ), mConn( NULL ) {} bool connect(); bool disconn(); protected: KxSymbol mServer; KxSymbol mUser; KxSymbol mPassword; KxSymbol mDatabase; MYSQL* mConn; }; #endif // !DBC_H
Now the .cpp file:
#include "dbc.h" bool DBConnector::connect() { mConn = mysql_init( NULL ); if ( !mConn ) return false; mConn = mysql_real_connect( mConn, mServer.string(), mUser.string(), mPassword.string(), mDatabase.string(), 0, NULL, 0 ); if ( !mConn ) return false; return true; } bool DBConnector::disconn() { if ( !mConn ) return false; mysql_close( mConn ); mConn = NULL; return true; }
So far, super simple. It just connects to a database. But when I link it into a short test program, I get the following link errors:
dbc.lib(dbc.obj) : error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function "public: bool __thiscall DBConnector::connect(void)" (?connect@DBConnector@@QAE_NXZ) dbc.lib(dbc.obj) : error LNK2019: unresolved external symbol _mysql_init@4 referenced in function "public: bool __thiscall DBConnector::connect(void)" (?connect@DBConnector@@QAE_NXZ ) dbc.lib(dbc.obj) : error LNK2019: unresolved external symbol _mysql_close@4 referenced in function "public: bool __thiscall DBConnector::disconn(void)" (?disconn@DBConnector@@QAE_NXZ )
Usually this problem is caused by not linking with the mysql libraries at all. You have to make sure that you link with libmysql.lib
and mysqlclient.lib
. But if you are on 64-bit windows, and install the 64-bit binaries, and then try to build 32-bit programs, you will get these link errors even if you link with the 64-bit libraries.
The answer is to download the 32-bit installer, and extract the 32-bit libraries. The 32-bit libraries can still communicate with a 64-bit database server.
When you run the program, make sure that libmysql.dll
is in the same directory as your program, or is in the dll search path. Also make sure that the version of libmysql.dll
that your program loads is the 32-bit version, or else the program will terminate immediately without issuing an error.
Resources:
Download MySQL Community Server | You can download 32-bit and 64-bit MySQL libraries here. |
MySQL Linkage Problems (C++) | A forum thread on linkage issues with MySQL |
C++ MySQL Beginner Tutorial | A very short tutorial on the basics of connecting to MySQL in C++ |
i was trying to make this long time ago.
i was trying with gcc mingw and was searching and searching and i didint find the problem till now.
finally the problem i solved downloading the library searching in google and the library that comes with mysql didnt fix my problem(but mysql was installed succesfully in my pc) so that was weird to me.
you dont know what grateful to you i am.
sorrry for my english im mexican :)
Thank you.After 2 hours to find the error. I got the answer:”But if you are on 64-bit windows, and install the 64-bit binaries, and then try to build 32-bit programs, you will get these link errors even if you link with the 64-bit libraries.”