2
votes

I gave myself a project to get familiar with Windows Programming and Visual Studio. I am having trouble linking to the MySQL libraries so that I can use it in my project. I keep getting a LNK2019 error, which most likely means that I am not linking the .lib files properly. I've already set its configuration to include the include directories and library directories. (I also tried different combinations of setting VC++ Directories and C/C++ / Linker.)

Error   1   error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function "public: void __thiscall Database::connect_db(void)" (?connect_db@Database@@QAEXXZ) D:\Media\Documents\Visual Studio 2010\Projects\swtPrice Server\swtPrice Server\Database.obj
Error   2   error LNK2019: unresolved external symbol _mysql_init@4 referenced in function "public: void __thiscall Database::connect_db(void)" (?connect_db@Database@@QAEXXZ)  D:\Media\Documents\Visual Studio 2010\Projects\swtPrice Server\swtPrice Server\Database.obj
Error   3   error LNK1120: 2 unresolved externals   D:\Media\Documents\Visual Studio 2010\Projects\swtPrice Server\Debug\swtPrice Server.exe    1

Here is a snippet of code that I copied online and trying to compile:

MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;

mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","bainm","not_telling","cpp_data",0,0,0);
if (connection == NULL) {
    //cout << mysql_error(&mysql) << endl;
    //return 1;
}

I configured my project to include the MySQL's include directory. This is how it looks like in the command line under the C/C++ tab:

/I"D:\Program Files\MySQL\MySQL Server 5.5\include" /ZI /nologo /W1 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\swtPrice Server.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue 

I configured my to include the MySQL's Library directory. This is how it looks like in the command line under the Linker:

/OUT:"D:\Media\Documents\Visual Studio 2010\Projects\swtPrice Server\Debug\swtPrice Server.exe" /NOLOGO /LIBPATH:"D:\Program Files\MySQL\MySQL Server 5.5\lib" "D:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /MANIFEST /ManifestFile:"Debug\swtPrice Server.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\Media\Documents\Visual Studio 2010\Projects\swtPrice Server\Debug\swtPrice Server.pdb" /SUBSYSTEM:CONSOLE /PGD:"D:\Media\Documents\Visual Studio 2010\Projects\swtPrice Server\Debug\swtPrice Server.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE

This is how the mySQL library directory looks like:

 Directory of D:\Program Files\MySQL\MySQL Server 5.5\lib

05/06/2011  10:38 AM    <DIR>          .
05/06/2011  10:38 AM    <DIR>          ..
05/06/2011  10:38 AM    <DIR>          debug
03/31/2011  04:53 PM         4,177,408 libmysql.dll
03/31/2011  04:53 PM            23,920 libmysql.lib
03/31/2011  04:52 PM        10,625,266 mysqlclient.lib
05/06/2011  10:38 AM    <DIR>          plugin

Any help is appreciated. Thank you in advance for any help.

1
Have you tried adding mysqlclient.lib to your build? Also, be sure to check you have the 32-bit libraries, I recall having a similar problem. - Liam M
That was it. I uninstalled mySQL and installed a 32-bit version, and my code compiled fine. Previously, I had a 64-bit version of mySQL, which was installed about a year ago. However, I am now running into "The program can't start because libmysql.dll is missing from your computer. Try reinstalling the program to fix this problem." Thank you for that piece of advice. - swtdrgn
Got it to run. Does anyone know how to specify a .dll file path instead of throwing the .dll in the same directory .exe? - swtdrgn
Aside from editing the environmental PATH variable, not that I'm aware of. If you place it in a directory structure adjacent to and below your executable, it will be searched. So, if you executable was at C:/my_app/bin.exe and the dll at C:/my_app/dll/libmysql.dll, that would be fine. - Liam M

1 Answers

3
votes

I ran in to the same problem of being getting link errors for all of the mysql api functions in VisualStudio2010. I confirmed the problem was due to using a 32-bit target in Visual Studio with a 64-bit build of MySQL. I added an x64 build to my build configuration and it linked without any errors