Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Okay, so I should probably start off by mentioning that I am using VS 2010 with the MySQL C API in two C++ projects. One project is an API static library I'm creating for providing quicker access to my MySQL database (using the api..), while the other project is a console app that will test the new library. Under both project properties I have set PROPERTIES \ CONFIGURATION PROPERTIES \ C/C++ \ ADDITIONAL INCLUDE LIBRARIES to include the MySQL folder containing the include files (MySQL \ Include). For the test app, I obviously include the header file to my static library as well.

In PROPERTIES \ CONFIGURATION PROPERTIES \ LINKER \ GENERAL, I have included the MySQL library directory (MySQL \ lib \ opt), as well as the static library file for my new library. I also added the necessary lib files for "Additional Dependencies" under "Input" as well for both programs. However, although my static library compiles fine, when I try compiling the test app I still get the following errors:

C++
Error	27	error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function "public: __thiscall Mainframe::User::User(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0User@Mainframe@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	E:\My Documents\Visual Studio 2010\Projects\Example\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	30	error LNK2019: unresolved external symbol _mysql_query@8 referenced in function "public: int __thiscall Mainframe::User::grabInfo(void)" (?grabInfo@User@Mainframe@@QAEHXZ)	E:\My Documents\Visual Studio 2010\Projects\Example\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	28	error LNK2019: unresolved external symbol _mysql_init@4 referenced in function "public: __thiscall Mainframe::User::User(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0User@Mainframe@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	E:\My Documents\Visual Studio 2010\Projects\Example\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	29	error LNK2019: unresolved external symbol _mysql_close@4 referenced in function "public: int __thiscall Mainframe::User::grabInfo(void)" (?grabInfo@User@Mainframe@@QAEHXZ)	E:\My Documents\Visual Studio 2010\Projects\Example\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	31	error LNK1120: 4 unresolved externals	1	E:\My Documents\Visual Studio 2010\Projects\Example\Debug\MSQLClientTest.exe	1	MSQLClientTest


Here's a sample of my library code and it's implementation:

server.h
C++
#include <vector>
#include <iostream>
#include <string>
#include <my_global.h>
#include <mysql.h>
namespace Mainframe
{
class User
{

public:
	
	Mainframe::User::User();
	Mainframe::User::User(std::string);

	int grabInfo();
};
}

Library:
C++
#include "stdafx.h"
#include "server.h"

    Mainframe::User::User() {}
    Mainframe::User::User(std::string id)
	{
	conn = mysql_init(NULL); //Obtain a connection handler

	//Connect to the server...
	mysql_real_connect(conn, "localhost", "root", "password", "users", 0, NULL, 0);
	}

	int Mainframe::User::grabInfo()
	{
		return mysql_query(conn, "SELECT * FROM users");
		mysql_close(conn);
	}


Test App:
C++
#include "stdafx.h"
#include "server.h"

int main(int argc, char* argv[])
{
	Mainframe::User u;
	std::cout << u.grabInfo();
	std::system("PAUSE");
	return 0;
}


Any idea what's causing these errors? A quick Google search reveals that this is a common issue, but not a single forum I looked at seemed to present an applicable solution. Any help would be greatly appreciated.

TIA!
Posted
Comments
Richard MacCutchan 17-Mar-13 4:39am    
I have come across something similar to this in the past. Can you show us the definitions of those functions as they exist in mysql.h?
Matthew Faithfull 17-Mar-13 8:22am    
It's worth noting that the your static library will always 'compile fine' in the sense that it will never show linker errors because fundamentally it isn't linked. It's perfectly valid to build a static library which is missing dependencies, these only have to become available when the static library is used to build an executable module, dll or exe.
However these dependencies do have to be available statically, i.e. you can't build an exe based on a static module that requires missing dependency funcX and then load the Dll that suplies funcX at runtime via LoadLibrary. You'll never get that far because the exe won't link.
Assuming you've got all this right then hopefully with additional info Richard can help you.
H.Brydon 17-Mar-13 14:57pm    
If you had provided this as a solution I would have +5'd you...
Matthew Faithfull 17-Mar-13 15:08pm    
Thanks but I'm not at all sure that's what the root problem is.
Linking is nasty like that it's not until you've got everything right except the one thing left that you have any real chance of fixing that one thing. If you jump the gun even by one step going after the errors it's telling you about you can go round in circles for days flip flopping between two or three incorrect configurations. Throw in incremental linking ( and a few bug in the MS linker ) to give you differing error sets for the same config depending on what came before and you can have a link nightmare. I once spent two weeks juggling 12 co-dependent Dlls before I got one to link completely. The other 11 only took 3 days :-)
H.Brydon 17-Mar-13 15:15pm    
[...would have +5'd that too.]

1 solution

Hey guys, after some research I finally found a solution. I had to first go to the MySQL website and download a 32 bit zip file for the C Connector API. I then extracted that to my own special directory, and referenced the lib folder and the include folder in their respective property windows in VS. I then added the reference to "mysqlclient.lib" and "libmysql.lib" in "Additional Dependencies". Setting the project build type to "Release", I copied the libmysql.dll file to my build path then built my project. Runs like a charm. I'll let you guys know if I hit any other issues. Thanks anyway.
 
Share this answer
 
Comments
Matthew Faithfull 17-Mar-13 20:15pm    
Good stuff, you should be able to set your own solution to accepted to mark this one as solved. If I need MySQL on Windows tips in the future I'll know who to ask. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900