c 连接mysql 驱动,C ++ / mysql连接器 - 未定义的引用get_driver_instance - 已经尝试过的简单的东西...

Yes this question has been asked before ... I've tried everything mentioned in the previous answers. My setup is really straightforward so this shouldn't be so hard.

I just want to program against mysql using C++. My source code is taken verbatem from the 'hello world' type example here:

I am on Ubuntu 12.10. I am trying:

g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18 -L/usr/lib/mysqlcppconn -lmysqlcppconn firsttry.cpp

It compiles (if I use -c option) but won't build, giving me the infamous:

/tmp/ccn768hj.o: In function `main':

firsttry.cpp:(.text+0x3a): undefined reference to `get_driver_instance'

A few details:

'firsttry.cpp' is just what I named the source code file, again taken verbatem from the official example

As you can see I AM linking in the mysqlclient library and the mysqlcppconn library. Many times when this question has been asked previously, the answer was to link those.

Some other historical answers suggest the sample source code is wrong and that the function in question needs to be in the sql::mysql namespace etc. I am pretty sure the source code is fine. Again, it compiles, and changing the namespaces in the source code just seems to make it worse.

Thank you in advance for any help you can provide.

解决方案

So I have now had this problem for a week now and I became very frustrated with it as well. I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. Here is what I have and I hope it helps.

I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with:

sudo apt-get install libmysqlcppconn-dev

And here is my simple tester source file "tester.cpp"

#include

#include

#include

#include

#include

#include

#include

using namespace sql;

int main(void){

sql::Driver *driver;

sql::Connection *con;

driver = get_driver_instance();

con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");

return 0;

}

And finally g++ compile command:

sudo g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn

This worked for me and I hope it helps you solve your problem!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值