Saturday 8 June 2019

Unresolved externals when trying to connect to mysql with C++ in Visual Studio 2013



I am trying to connect to mysql with C++ in Visual Studio 2013. I am using Release Configuration. I already put the directories



C:\mysql-5.7.11-winx64\lib
C:\Program Files\MySQL\Connector.C++ 1.1\include
C:\boost_1_60_0



in C/C++ general-->Additional Dependencies, VC++ Directories and Linker general->Additional Dependencies



and I also added mysqlcppconn-static.lib and libmysql.lib on Linker-->Input.



And now I am getting an error of
error LNK1120: 2 unresolved externals
error LNK2001: unresolved external symbol _get_driver_instance
error LNK2001: unresolved external symbol _WinMain@16



How do I fix this? Thanks!



/* Standard C++ includes */
#include
#include

/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include
#include
#include
#include
#include

using namespace std;

int main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;

try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;

/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");

stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT)");
delete stmt;

/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->executeUpdate();
}
delete pstmt;

/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();

/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
delete res;

delete pstmt;
delete con;

} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "
<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

Answer



You've got a couple of problems occurring.




  1. The first is that your project settings appear to be expecting a Windows application, not a console application, as evidenced by trying to find the symbol _WinMain@16 instead of main. Double-check your project settings.


  2. The second is that you're trying to link statically, but at least in some versions of mysql, you need to define a preprocessor directive in order for symbols to be visible to the rest of your code. Specifically, _get_driver_instance(), since this was a function added for those linking against the mysql libraries dynamically. Try defining mysqlcppconn_EXPORTS in the project settings and rebuild.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...