[英]MySQL C++ Connector: undefined reference to `get_driver_instance'
I've been trying to get the MySQL connector working I've installed both the connector and the mysql client library but I am still getting this error:我一直在尝试让 MySQL 连接器正常工作我已经安装了连接器和 mysql 客户端库,但我仍然收到此错误:
obj/Database.obj: In function `Database::connect()':
/home/xeross/alpine/src/server/Database.cpp:13: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status
make[2]: *** [alpine-server] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
Using Ubuntu 10.04 And my makefile is as follows:使用 Ubuntu 10.04 而我的 makefile 如下:
INCLUDES = -I./src -I./src/shared
OUTDIR = bin
INTDIR = obj
OPTIONS = -ggdb -g3 -Wall -O0
alpine-server : Shared.a AsyncServerSocket.obj PlayerHandler.obj PacketHandler.obj Session.obj User.obj Database.obj init
g++ $(INCLUDES) $(OPTIONS) -static \
-pthread \
-lmysqlcppconn-static \
-o $(OUTDIR)/alpine-server src/server/main.cpp \
$(INTDIR)/AsyncServerSocket.obj \
$(INTDIR)/PacketHandler.obj \
$(INTDIR)/Database.obj \
$(INTDIR)/PlayerHandler.obj \
$(INTDIR)/Session.obj \
$(INTDIR)/User.obj \
$(INTDIR)/Shared.a \
-lboost_system \
-lmysqlclient
AsyncServerSocket.obj : src/server/AsyncServerSocket.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/AsyncServerSocket.obj src/server/AsyncServerSocket.cpp
PlayerHandler.obj : src/server/PlayerHandler.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/PlayerHandler.obj src/server/PlayerHandler.cpp
PacketHandler.obj : src/server/PacketHandler.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/PacketHandler.obj src/server/PacketHandler.cpp
Session.obj : src/server/Session.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Session.obj src/server/Session.cpp
User.obj : src/server/User.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/User.obj src/server/User.cpp
Database.obj : src/server/Database.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Database.obj src/server/Database.cpp
# Shared.a
Shared.a : Packet.obj Flags.obj AsyncSocket.obj Log.obj init
ar -cvq $(INTDIR)/Shared.a \
$(INTDIR)/Packet.obj \
$(INTDIR)/Flags.obj \
$(INTDIR)/AsyncSocket.obj \
$(INTDIR)/Log.obj
ranlib $(INTDIR)/Shared.a
Packet.obj : src/shared/packet.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Packet.obj src/shared/packet.cpp
Flags.obj : src/shared/Flags.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Flags.obj src/shared/Flags.cpp
AsyncSocket.obj : src/shared/AsyncSocket.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/AsyncSocket.obj src/shared/AsyncSocket.cpp
Log.obj : src/shared/Log.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Log.obj src/shared/Log.cpp
init:
mkdir -p bin obj
clean:
rm -f $(INTDIR)/*.obj $(INTDIR)/*.a
The Code编码
// Excerpt from .hpp file
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
// End excerpt
void Database::connect()
{
std::stringstream connString;
connString << "tcp://";
connString << m_host;
connString << ":";
connString << m_port;
m_driver = get_driver_instance(); // This guy is being a *****
m_conn = m_driver->connect(connString.str(), m_user, m_password);
m_conn->setSchema(m_database);
}
What can I do to fix this ?我能做些什么来解决这个问题?
Finally I could successfully compile a program with C++ connector in Ubuntu 10.10. 最后,我可以在Ubuntu 10.10中使用C ++连接器成功编译程序。
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. 最初我遇到了同样的问题“未定义引用`get_driver_instance'”来解决这个问题我声明了MySQL_Driver类型的驱动程序实例变量。 For ready reference this type is defined in mysql_driver.h file.
对于准备参考,此类型在mysql_driver.h文件中定义。 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 我用-l mysqlcppconn链接器选项编译了程序
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 . 我在64位CentOS上使用Eclipse CDT,对于任何阅读此处的人都是以下步骤。
include "mysql_driver.h"
include "mysql_connection.h"
using namespace sql::mysql;
Be sure that in Eclipse you have specified in your Eclipse project settings. 确保在Eclipse中您已在Eclipse项目设置中指定。 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
. 您的
mysql/include
的mysql/include
和mysql/include/cppconn
目录,以及库目录中的mysql/lib
,然后更重要的是指定-lmysqlcppconn
。
Be sure you got the -m64 set in the Eclipse compiler options too. 确保在Eclipse编译器选项中也设置了-m64。
When you run your program, it may complain of missing libmysqlcppconn.so.1
. 当您运行程序时,它可能会抱怨缺少
libmysqlcppconn.so.1
。 Do this, copy libmysqlcppconn.so.1.0.5
in your /usr/lib64
directory. 执行此操作,在
/usr/lib64
目录中复制libmysqlcppconn.so.1.0.5
。 Make a link of libmysqlcppconn.so.1
towards libmysqlcppconn.so.1.0.5
within that directory. 在该目录中为
libmysqlcppconn.so.1
libmysqlcppconn.so.1.0.5
的链接。
Your program should now run. 你的程序现在应该运行。
The code would be more helpful than the make file but try adding using namespace sql;
代码比make文件更有用,但尝试
using namespace sql;
添加using namespace sql;
to the top of Database.cpp. 到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
在使用该库中的东西的目标文件之后 ,您需要添加-lmysqlcppconn-static
。
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 . 第一个库包含/ usr / include / cppconn /中头文件的代码,第二个库包含头文件mysql_driver.h和mysql_connection.h中的代码。
To me was needed -lmysqlclient对我来说需要 -lmysqlclient
g++ -o mariaS2json test.cpp -L/usr/lib/ -lmysqlcppconn -lmysqlclient
And now all works现在一切正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.