Mick Newman
Mick Newman

Reputation: 23

Can't Link MySQL in a C++ Program in Visual Studio 2015

I am trying to compile a very simple program to just test my MySQL installation with a simple DB Table. I am new to MySQL and relatively inexperienced programmer so it may be a very simple set up/linking problem:

#include <iostream>
#include <memory>
#include <string> 

#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>

using namespace std;
using namespace sql;

#define DBHOST "tcp://127.0.0.1:3306"
#define USER "root"
#define PASSWORD ""
#define DATABASE "mydb"

int main()
{
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;

std::string user(USER);
std::string dbhost(DBHOST);
std::string password(PASSWORD);

driver = get_driver_instance();
//con = driver->connect(dbhost, user, password);
//con->setSchema(DATABASE);

return 0;
}

I have installed the latest MySQL package and have the include and library files at the following locations: C:\Program Files\MySQL\Connector.C++ 1.1\include C:\Program Files\MySQL\Connector.C++ 1.1\lib\opt

I have added the above include path to the "Include Directories" under the VC+= Directories menu in the Configuration properties as well as the above library path in the "Library Directories" on the same tab.

Additionally I have added mysqlcppconn.lib as an "Additional Dependency in the Linker-Input page.

When I compile I get a LNK 2019 "unresolved external symbol __imp__get_driver_instance referenced in function _main" error......

I suspect it is something fundamental that I am unaware of !!!

Upvotes: 1

Views: 1928

Answers (1)

Mick Newman
Mick Newman

Reputation: 23

OK. So after another day of playing around I moved the header files and libraries for both of the places where MySQL was installed (the default Program Files directory and the Program Files (x86) directory) into a MySQL/include and MySQL/lib as well as MySQL(x86)/include and MySQL(x86)/lib in my projects directory.

I tried linking to these in succession in the respective places in Visual Studio 2015. The program compiled linking to the x86 versions.

The only thing I didn't realise was that I required the mysqlcppconn.dll in the run directory of the project to load correctly. Once I had Googled this the program loaded and ran successfully !!

Upvotes: 0

Related Questions