Ubuntu下Boost库连接mysql

1、Boost库的编译和安装

1.1 Boost库安装包下载

通过wget工具下载,指令如下

wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.gz

1.2 Boost库解压缩

在下载目录下,运行指令

tar zxvf boost_1_64_0.tar.gz

1.3 Boost库的编译

进入解压缩后目录

cd boost_1_64_0

运行bootstrap.sh脚本并设置参数:

./bootstrap.sh --with-libraries=all --with-toolset=gcc

--with-libraries指定编译哪些boost库,all的话就是全部编译。
--with-toolset指定编译时使用哪种编译器,Ubuntu下使用gcc即可。
命令执行完成后看到显示如下即为成功:

Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.6
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Generating Boost.Build configuration in project-config.jam...
Bootstrapping is done. To build, run:./b2
To adjust configuration, edit 'project-config.jam'.
Further information:- Command line help:
./b2 --help
- Getting started guide: http://www.boost.org/more/getting_started/unix-variants.html
- Boost.Build documentation:http://www.boost.org/build/doc/html/index.html

执行以下命令开始进行boost的编译:

./b2 toolset=gcc

编译的时间比较久,耐心等待,结束后会有以下提示:

ln-UNIX stage/lib/libboost_wave.so
...failed updating 56 targets...
...skipped 6 targets...
...updated 1081 targets...

1.4 Boost库的安装

执行以下命令开始安装boost:

./b2 install --prefix=/usr

--prefix=/usr用来指定boost的安装目录,不加此参数的话默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。这里把安装目录指定为--prefix=/usr则boost会直接安装到系统头文件目录和库文件目录下,可以省略配置环境变量。
安装完毕后会有以下提示:

...failed updating 56 targets...
...skipped 6 targets...
...updated 13610 targets...

安装后使用boost库进行编译,还需要执行一下这个命令:

ldconfig

至此Boost库安装完成

2、Mysql Connector的安装

2.1 下载mysql connector的头文件和库,下载地址为

http://www.mysql.com/downloads/connector/cpp/

注意下载与系统版本对应的版本

2.2 将文件解压缩

然后将文件夹中的include中的文件和lib中的文件分别拷到/usr/include和/usr/lib中

cp -r * /usr/include
cp -r * /usr/lib

测试代码

#include <iostream>  
#include <mysql/mysql.h>
  
using namespace std;  
  
#include <mysql_connection.h>  
#include <mysql_driver.h>  
#include <cppconn/driver.h>  
  
using namespace sql;  
  
#define DBHOST "tcp://139.199.179.66:3306"  
#define USER "root"  
#define PASSWORD "*****"  
  
int main() {  
   Driver *driver;  
   Connection *conn;  
   driver = mysql::get_mysql_driver_instance();
   conn = driver->connect(DBHOST, USER, PASSWORD);  
   conn->setAutoCommit(0);  
   cout<<"DataBase connection autocommit mode = "<<conn->getAutoCommit()<<endl;  
   delete conn;  
   driver = NULL;  
   conn = NULL;  
   cout<<"finish"<<endl;
   return 0;  
} 

posted on 2018-07-16 19:40  iDea2011  阅读(317)  评论(0)    收藏  举报

导航