Linux/Boost/mysql-connecter安装
linux平台下要编译安装除gcc和gcc-c++之外,还需要两个开发库:bzip2-devel 和python-devel,因此在安装前应该先保证这两个库已经安装:
#yum install gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel -y
然后是去官网下载源码包,地址下载,解压,按照如下步骤:
#tar xvzf boost_1_50_0.tar.gz
进入boost_1_50_0目录:#cd boost_1_50_0
然后是编译安装,boost源码包中有配置脚本,直接用就可以:
#sh ./bootstrap.sh
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2
DetectingPython version...2.6
DetectingPython root.../usr
Unicode/ICU support forBoost.Regex?...not found.
GeneratingBoost.Build configuration in project-config.jam...
Bootstrappingisdone.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/boost-build2/doc/html/index.html
#./b2
然后就是漫长的等待,如果最后出现:TheBoost C++Libraries were successfully built!
The following directory should be added to compiler include paths:
/home/gang/BAK/boost_1_50_0
The following directory should be added to linker library paths:
/home/gang/BAK/boost_1_50_0/stage/lib
最后就是安装:
#./b2 install --prefix=/usr/local
安装后的头文件在/usr/local/include/boost里面,而相应的库在/usr/local/lib/libboost_*(1)使用C++版本的mysql connector首先需要安装和编译boost库。
可以在boost官网上下载源文件,自己进行编译。也可以直接使用命令下载和编译,具体命令为:
apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-*
(2)然后需要下载mysql connector的头文件和库,
下载地址为:
http://www.mysql.com/downloads/connector/cpp/
(3)下载以后将文件进行解压
然后将文件夹中的include中的文件和lib中的文件分别拷到/usr/include和/usr/lib中。
(4)使用代码测试(testmysql.cpp):
- #include <iostream>
- #include <sstream>
- #include <memory>
- #include <string>
- #include <stdexcept>
- using namespace std;
- #include <mysql_connection.h>
- #include <mysql_driver.h>
- #include <cppconn/driver.h>
- using namespace sql;
- #define DBHOST "tcp://127.0.0.1:3306"
- #define USER "root"
- #define PASSWORD "your password here"
- int main() {
- Driver *driver;
- Connection *conn;
- driver = get_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;
- return 0;
- }
然后对该文件进行编译:g++ -o testmysql -lmysqlcppconn testmysql.cpp
注意编译时不要忘了使用-lmysqlcppconn。