MySQL C++连接器:对“get_driver_instance”的未定义引用
我一直在尝试让 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
使用 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
代码
// 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);
}
我能做什么来解决这个问题?
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:
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:
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最后我可以在 Ubuntu 10.10 中使用 C++ 连接器成功编译程序。
最初,我遇到了同样的问题,“未定义引用‘get_driver_instance’”,为了解决这个问题,我声明了 MySQL_Driver 类型的驱动程序实例变量。为了便于参考,该类型在 mysql_driver.h 文件中定义。这是我在程序中使用的代码片段。
我使用 -l mysqlcppconn 链接器选项编译了程序
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.
and I compiled the program with -l mysqlcppconn linker option
非常感谢,我也修好了。我有确切的经历。
我在 64 位 CentOS 上使用 Eclipse CDT,对于阅读本文的任何人,请执行以下步骤。
include "mysql_driver.h"
include "mysql_connection.h"
using namespace sql::mysql;
确保在 Eclipse 中您有在 Eclipse 项目设置中指定。
include 中的
mysql/include
和mysql/include/cppconn
目录,然后是库目录中的mysql/lib
,然后等等重要的是,您指定了-lmysqlcppconn
。确保您在 Eclipse 编译器选项中也设置了 -m64。
当您运行程序时,它可能会抱怨缺少
libmysqlcppconn.so.1
。为此,请将libmysqlcppconn.so.1.0.5
复制到/usr/lib64
目录中。在该目录中将libmysqlcppconn.so.1
链接到libmysqlcppconn.so.1.0.5
。您的程序现在应该可以运行了。
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 .
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.
the
mysql/include
andmysql/include/cppconn
directories in your include and then also themysql/lib
in the library directory, then and more importantly you specify-lmysqlcppconn
.Be sure you got the -m64 set in the Eclipse compiler options too.
When you run your program, it may complain of missing
libmysqlcppconn.so.1
. Do this, copylibmysqlcppconn.so.1.0.5
in your/usr/lib64
directory. Make a link oflibmysqlcppconn.so.1
towardslibmysqlcppconn.so.1.0.5
within that directory.Your program should now run.
您需要链接
第一个库包含 /usr/include/cppconn/ 中标头的代码,第二个库包含标头 mysql_driver.h 中找到的代码> 和 mysql_connection.h。
You need to link with
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.
您需要在使用该库内内容的对象文件之后添加
-lmysqlcppconn-static
。You'll need to add
-lmysqlcppconn-static
after the object files that uses stuff inside that library.该代码比 make 文件更有帮助,但请尝试将
using namespace sql;
添加到 Database.cpp 的顶部。The code would be more helpful than the make file but try adding
using namespace sql;
to the top of Database.cpp.对我来说需要 -lmysqlclient
现在一切正常
To me was needed -lmysqlclient
And now all works