Linux/Boost/mysql-connecter安装

Linux 编译安装Boost  

2012-07-24 17:07:20|  分类: Linux/C++及其他 |  标签:linux  boost  编译  安装   |字号 订阅

 
如果是windows平台,请参考:链接

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

表示编译成功,如果没有成功,就需要回查看哪里出现error,再安装相应的库,
最后就是安装:

#./b2 install --prefix=/usr/local

安装后的头文件在/usr/local/include/boost里面,而相应的库在/usr/local/lib/libboost_*
 
mysql-connector安装
 

(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):

        

  1. #include <iostream>  
  2. #include <sstream>  
  3. #include <memory>  
  4. #include <string>  
  5. #include <stdexcept>  
  6.   
  7. using namespace std;  
  8.   
  9. #include <mysql_connection.h>  
  10. #include <mysql_driver.h>  
  11. #include <cppconn/driver.h>  
  12.   
  13. using namespace sql;  
  14.   
  15. #define DBHOST "tcp://127.0.0.1:3306"  
  16. #define USER "root"  
  17. #define PASSWORD "your password here"  
  18.   
  19. int main() {  
  20.    Driver *driver;  
  21.    Connection *conn;  
  22.    driver = get_driver_instance();  
  23.    conn = driver->connect(DBHOST, USER, PASSWORD);  
  24.    conn->setAutoCommit(0);  
  25.    cout<<"DataBase connection autocommit mode = "<<conn->getAutoCommit()<<endl;  
  26.    delete conn;  
  27.    driver = NULL;  
  28.    conn = NULL;  
  29.    return 0;  
  30. }  

然后对该文件进行编译:g++ -o testmysql -lmysqlcppconn testmysql.cpp

注意编译时不要忘了使用-lmysqlcppconn。

 

posted @ 2014-01-08 09:22  孤火  阅读(1336)  评论(2编辑  收藏  举报