MySQL C++ Connector: undefined reference to `get_driver_instance'

有些话、适合烂在心里 提交于 2019-11-30 09:48:32
Supratim Dey

Finally I could successfully compile a program with C++ connector in Ubuntu 10.10.

Initially I faced the same problem with "undefined reference to `get_driver_instance' " to solve this I declare my driver instance variable of MySQL_Driver type. For ready reference this type is defined in mysql_driver.h file. Here is the code snippet I used in my program.

sql::mysql::MySQL_Driver *driver;
try {     
    driver = sql::mysql::get_driver_instance();
}

and I compiled the program with -l mysqlcppconn linker option

mysqlforums

Thank you so much, I got it fixed too. I had the exact experience.

I am using Eclipse CDT on 64 Bit CentOS and for anyone reading this here are the following steps .

  1. first, be sure that the following are in the code.

include "mysql_driver.h"

include "mysql_connection.h"

using namespace sql::mysql;

  1. Be sure that in Eclipse you have specified in your Eclipse project settings. the mysql/include and mysql/include/cppconn directories in your include and then also the mysql/lib in the library directory, then and more importantly you specify -lmysqlcppconn.

  2. Be sure you got the -m64 set in the Eclipse compiler options too.

  3. When you run your program, it may complain of missing libmysqlcppconn.so.1 . Do this, copy libmysqlcppconn.so.1.0.5 in your /usr/lib64 directory. Make a link of libmysqlcppconn.so.1 towards libmysqlcppconn.so.1.0.5 within that directory.

Your program should now run.

You'll need to add -lmysqlcppconn-static after the object files that uses stuff inside that library.

The code would be more helpful than the make file but try adding using namespace sql; to the top of Database.cpp.

// Excerpt from .hpp file
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace sql;     // <---- add here

You need to link with

-lmysqlcppconn -lmysqlcppconn-static

The first library contains the code for the headers in /usr/include/cppconn/, and the second library contains the code found in the headers mysql_driver.h and mysql_connection.h.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!