ubuntu16.04安装soci(一个可以封装访问oracle、mysql和postgresql等各种数据库的包)

来源:互联网 发布:42u网络机柜价格 编辑:程序博客网 时间:2024/05/29 15:05

安装一个东西首先要去看他的官网,这是它的官网http://soci.sourceforge.net/,但是似乎现在不翻墙连不上去(17-08-13),翻墙去看。

1。安装依赖环境

在下载安装之前,看它的requirements,需要安装哪些库,见下方:
Below is an overall list of SOCI core:
● C++ compiler: GCC, Microsoft Visual C++, LLVM/clang
● CMake 2.8+ - in order to use build configuration for CMake
● Boost C++ Libraries: DateTime, Fusion, Optional, Preprocessor, Tuple
and backend-specific dependencies:
● DB2 Call Level Interface (CLI)
● Firebird client library
● mysqlclient - C API to MySQL
● ODBC (Open Database Connectivity) implementation: Microsoft ODBC, iODBC, unixODBC
● Oracle Call Interface (OCI)
● libpq - C API to PostgreSQL
● SQLite 3 library
核心的(core)必须安装,backend-specific depedencies根据需要安装;

2。安装soci

直接看document这个页面,他说可以在github上clone下载或者在他给的指定链接下载,我是用的github上的源码编译安装的:git clone git://github.com/SOCI/soci.git
接下来的步骤:
cmake -DWITH_BOOST=ON -DSOCI_TESTS=ON -DWITH_SQLITE3=ON
make
make test #这里的failed不用理会
sudo make install

3。 测试
挑一个目录,创建你的makefile和main.cpp
内容如下:
makefile:

g++ -Wall -g -o main main.cpp  -I/usr/include/mysql -lsoci_core -lsoci_mysql ```

main.cpp如下:

#include <soci/soci.h>#include <soci/mysql/soci-mysql.h>#include<iostream>#include<istream>#include<ostream>#include<string>#include<exception>using namespace std;using namespace soci;int main() {    try {        session sql(mysql, "service=student user=root password=123");        int count ;        sql<< "select count(*) from student_info", into(count);        cout<<"count="<<count<<endl;    } catch (exception const &e) {        cerr<<"Error:" <<e.what()<<endl;    }}

运行时出现的问题:

(1):一开始包含的头文件是#include<soci.h>找不到soci.h,在终端输入locate soci.h发现在它在这里:/usr/local/include/soci/soci.h;所以就将#include<soci.h>改为了#include<soci/soci.h>这样就找到了;以后出现的类似找不到的头文件都用这种方法解决。(2):第二个问题是undefined reference to `soci::mysql'错误,原来是我在makefile文件里没加-lsoci_core,于是,出现此类的错误均可以由此解决;(3):第三个问题是/usr/bin/ld: cannot find -lsoci-core;/usr/bin/ld: cannot find -lsoci-mysql出现这个问题, 查看这里:(usr/bin/ld: cannot find 错误解决方法);这是连接器错误,要保证链接器是拼写正确的-lsoci_core不要下写成-lsoci-core最后,终于可以编译运行通过拉!

参考:http://soci.sourceforge.net/;
https://sourceforge.net/p/soci/mailman/soci-users/?viewmonth=200709;
https://github.com/avplayer/avrouter/wiki/%E5%85%B3%E4%BA%8E%E6%90%AD%E5%BB%BAsoci%E5%92%8Cpostgresql%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83%E7%9A%84%E6%AD%A5%E9%AA%A4—-ubuntu;

由于国内看不了官网,下面我粘贴的官网的安装页面:

SOCI - The C++ Database Access Library
Installation
Requirements
Downloading
Building using CMake
On Unix
On Windows
Building using classic Makefiles on Unix (deprecated)
Running Tests
Usage
Requirements
Below is an overall list of SOCI core:
● C++ compiler: GCC, Microsoft Visual C++, LLVM/clang
● CMake 2.8+ - in order to use build configuration for CMake
● Boost C++ Libraries: DateTime, Fusion, Optional, Preprocessor, Tuple
and backend-specific dependencies:
● DB2 Call Level Interface (CLI)
● Firebird client library
● mysqlclient - C API to MySQL
● ODBC (Open Database Connectivity) implementation: Microsoft ODBC, iODBC, unixODBC
● Oracle Call Interface (OCI)
● libpq - C API to PostgreSQL
● SQLite 3 library
Downloading
Download package with latest release of the SOCI source code: soci-X.Y.Z, where X.Y.Z is the version number. Unpack the archive.
You can always clone SOCI from the Git repository:
git clone git://github.com/SOCI/soci.git
Building using CMake
SOCI is configured to build using CMake system in version 2.8+.
The build configuration allows to control various aspects of compilation and installation by setting common CMake variables that change behaviour, describe system or control build (see CMake help) as well as SOCI-specific variables described below. All these variables are available regardless of platform or compilation toolset used.
Running CMake from the command line allows to set variables in the CMake cache with the following syntax: -DVARIABLE:TYPE=VALUE. If you are new to CMake, you may find the tutorial Running CMake helpful.
The following tables provide summary of variables accepted by CMake scripts configuring SOCI build. The lists consist of common variables for SOCI core and all backends as well as variables specific to SOCI backends and their direct dependencies.
CMAKE_BUILD_TYPEstringSpecifies the build type for make based generators (see CMake help).CMAKE_INSTALL_PREFIXpathInstall directory used by install command (see CMake help).CMAKE_VERBOSE_MAKEFILEbooleanIf ON, create verbose makefile (see CMake help).
SOCI_STATICbooleanRequest to build static libraries, along with shared, of SOCI core and all successfully configured backends.SOCI_TESTSbooleanRequest to build regression tests for SOCI core and all successfully configured backends.WITH_BOOSTbooleanShould CMake try to detect Boost C++ Libraries. If ON, CMake will try to find Boost headers and binaries of Boost.Date_Time library.
IBM DB2
WITH_DB2booleanShould CMake try to detect IBM DB2 Call Level Interface (CLI) library.DB2_INCLUDE_DIRstringPath to DB2 CLI include directories where CMake should look for sqlcli1.h header.DB2_LIBRARIESstringFull paths to db2 or db2api libraries to link SOCI against to enable the backend support.SOCI_DB2booleanRequests to build DB2 backend. Automatically switched on, if WITH_DB2 is set to ON.SOCI_DB2_TEST_CONNSTRstringSee DB2 backend refernece for details. Example: -DSOCI_DB2_TEST_CONNSTR:STRING=”DSN=SAMPLE;Uid=db2inst1;Pwd=db2inst1;autocommit=off”
Firebird
WITH_FIREBIRDbooleanShould CMake try to detect Firebird client library.FIREBIRD_INCLUDE_DIRstringPath to Firebird include directories where CMake should look for ibase.h header.FIREBIRD_LIBRARIESstringFull paths to Firebird fbclient or fbclient_ms libraries to link SOCI against to enable the backend support.SOCI_FIREBIRDbooleanRequests to build Firebird backend. Automatically switched on, if WITH_FIREBIRD is set to ON.SOCI_FIREBIRD_TEST_CONNSTRstringSee Firebird backend refernece for details. Example: -DSOCI_FIREBIRD_TEST_CONNSTR:STRING=”service=LOCALHOST:/tmp/soci_test.fdb user=SYSDBA password=masterkey”
MySQL
WITH_MYSQLbooleanShould CMake try to detect mysqlclient libraries providing MySQL C API. Note, currently the mysql_config program is not being used.MYSQL_DIRstringPath to MySQL installation root directory. CMake will scan subdirectories MYSQL_DIR/include and MYSQL_DIR/librespectively for MySQL headers and libraries.MYSQL_INCLUDE_DIRstringPath to MySQL include directory where CMake should look for mysql.h header.MYSQL_LIBRARIESstringFull paths to libraries to link SOCI against to enable the backend support.SOCI_MYSQLbooleanRequests to build MySQL backend. Automatically switched on, if WITH_MYSQL is set to ON.SOCI_MYSQL_TEST_CONNSTRstringConnection string to MySQL test database. Format of the string is explained MySQL backend refernece. Example: -DSOCI_MYSQL_TEST_CONNSTR:STRING=”db=mydb user=mloskot password=secret”
ODBC
WITH_ODBCbooleanShould CMake try to detect ODBC libraries. On Unix systems, CMake tries to find unixODBC or iODBC implementations.ODBC_INCLUDE_DIRstringPath to ODBC implementation include directories where CMake should look for sql.h header.ODBC_LIBRARIESstringFull paths to libraries to link SOCI against to enable the backend support.SOCI_ODBCbooleanRequests to build ODBC backend. Automatically switched on, if WITH_ODBC is set to ON.SOCI_ODBC_TEST_{database}_CONNSTRstringODBC Data Source Name (DSN) or ODBC File Data Source Name (FILEDSN) to test database: Microsoft Access (.mdb), Microsoft SQL Server, MySQL, PostgreSQL or any other ODBC SQL data source. {database} is placeholder for name of database driver ACCESS, MYSQL, POSTGRESQL, etc. See ODBC backend refernece for details. Example: -DSOCI_ODBC_TEST_POSTGRESQL_CONNSTR=”FILEDSN=/home/mloskot/dev/soci/_git/build/test-postgresql.dsn”
Oracle
WITH_ORACLEbooleanShould CMake try to detect Oracle Call Interface (OCI) libraries.ORACLE_INCLUDE_DIRstringPath to Oracle include directory where CMake should look for oci.h header.ORACLE_LIBRARIESstringFull paths to libraries to link SOCI against to enable the backend support.SOCI_ORACLEbooleanRequests to build Oracle backend. Automatically switched on, if WITH_ORACLE is set to ON.SOCI_ORACLE_TEST_CONNSTRstringConnection string to Oracle test database. Format of the string is explained Oracle backend refernece. Example: -DSOCI_ORACLE_TEST_CONNSTR:STRING=”service=orcl user=scott password=tiger”
PostgreSQL
WITH_POSTGRESQLbooleanShould CMake try to detect PostgreSQL client interface libraries. SOCI relies on libpq C library.POSTGRESQL_INCLUDE_DIRstringPath to PostgreSQL include directory where CMake should look for libpq-fe.h header.POSTGRESQL_LIBRARIESstringFull paths to libraries to link SOCI against to enable the backend support.SOCI_POSTGRESQLbooleanRequests to build PostgreSQL backend. Automatically switched on, if WITH_POSTGRESQL is set to ON.SOCI_POSTGRESQL_TEST_CONNSTRbooleanConnection string to PostgreSQL test database. Format of the string is explained PostgreSQL backend refernece. Example: -DSOCI_POSTGRESQL_TEST_CONNSTR:STRING=”dbname=mydb user=mloskot”
SQLite 3
WITH_SQLITE3booleanShould CMak try to detect SQLite C/C++ library. As bonus, the configuration tries OSGeo4W distribution if OSGEO4W_ROOTenvironment variable is set.SQLITE_INCLUDE_DIRstringPath to SQLite 3 include directory where CMake should look for sqlite3.h header.SQLITE_LIBRARIESstringFull paths to libraries to link SOCI against to enable the backend support.SOCI_SQLITE3booleanRequests to build SQLite3 backend. Automatically switched on, if WITH_SQLITE3 is set to ON.SOCI_SQLITE3_TEST_CONNSTRstringConnection string is simply a file path where SQLite3 test database will be created (e.g. /home/john/soci_test.db). Check SQLite3 backend refernece for details. Example: -DSOCI_SQLITE3_TEST_CONNSTR=”my.db”
Empty (sample backend)
SOCI_EMPTYbooleanBuilds the sample backend called Empty. Always ON by default.SOCI_EMPTY_TEST_CONNSTRstringConnection string used to run regression tests of the Empty backend. It is a dummy value. Example: -DSOCI_EMPTY_TEST_CONNSTR=”dummy connection”
By default, CMake will try to determine availability of all depdendencies automatically. If you are lucky, you will not need to specify any of the CMake variables explained above. However, if CMake reports some of the core or backend-specific dependencies as missing, you will need specify relevant variables to tell CMake where to look for the required components.
CMake configures SOCI build performing sequence of steps. Each subsequent step is dependant on result of previous steps corresponding with particular feature. First, CMake checks system platform and compilation toolset. Next, CMake tries to find all external dependencies. Then, depending on the results of the dependency check, CMake determines SOCI backends which are possible to build. The SOCI-specific variables described above provide users with basic control of this behaviour.
Building using CMake on Unix
Short version using GNU Make makefiles:
mkdirbuild cd build
cmakeGUnixMakefilesDWITHBOOST=OFFDWITHORACLE=OFF()../sociX.Y.Z make
$ make install

Building using CMake on Windows
Short version using Visual Studio 2010 and MSBuild:
C:>MKDIR build
C:>cd build
C:\build>cmake -G “Visual Studio 10” -DWITH_BOOST=OFF -DWITH_ORACLE=OFF (…) ..\soci-X.Y.Z
C:\build>msbuild.exe SOCI.sln

Building using classic Makefiles on Unix (deprecated)
NOTE: These Makefiles have not been maintained for long time. The officially maintained build configuration is CMake. If you still want to use these Makefiles, you’ve been warned that you may need to patch them.
The classic set of Makefiles for Unix/Linux systems is provided for those users who need complete control over the whole process and who can benefit from the basic scaffolding that they can extend on their own. In this sense, the basic Makefiles are supposed to provide a minimal starting point for custom experimentation and are not intended to be a complete build/installation solution.
At the same time, they are complete in the sense that they can compile the library with all test programs and for some users this level of support will be just fine.
The core directory of the library distribution contains the Makefile.basic that can be used to compile the core part of the library. Run make -f Makefile.basic or make -f Makefile.basic shared to get the static and shared versions, respectively. Similarly, the backends/name directory contains the backend part for each supported backend with the appropriate Makefile.basic and the backends/name/test directory contains the test program for the given backend.
For example, the simplest way to compile the static version of the library and the test program for PostgreSQL is:
cdsrc/core make -f Makefile.basic
cd../backends/postgresql make -f Makefile.basic
cdtest make -f Makefile.basic

Note: For each backend and its test program, the Makefile.basics contain the variables that can have values specific to the given environment - they usually name the include and library paths. These variables are placed at the beginning of the Makefile.basics. Please review their values in case of any compilation problems.
The Makefiles for test programs can be a good starting point to find out correct compiler and linker options.
Running regression tests
The process of running regression tests highly depends on user’s environment and build configuration, so it may be quite involving process. The CMake configuration provides variables to allow users willing to run the tests to configure build and specify database connection parameters (see the tables above for variable names).
In order to run regression tests, configure and build desired SOCI backends and prepare working database instances for them.
While configuring build with CMake, specify SOCI_TESTS=ON to enable building regression tests. Also, specify SOCI_{backend name}_TEST_CONNSTR variables to tell the tests runner how to connect with your test databases.
Dedicated make test target can be used to execute regression tests on build completion:
mkdirbuild cd build
cmakeGUnixMakefilesDWITHBOOST=OFF DSOCITESTS=ON DSOCIEMPTYTESTCONNSTR=dummyconnection DSOCISQLITE3TESTCONNSTR=test.db ()../sociX.Y.Z make
maketest make install

In the example above, regression tests for the sample Empty backend and SQLite 3 backend are configured for execution by make test target.
Libraries usage
CMake build produces set of shared and static libraries for SOCI core and backends separately. On Unix, for example, build/lib directory will consist of the static libraries named like libsoci_core.a, libsoci_sqlite3.a and shared libraries with names like libsoci_core.so.3.2.3, libsoci_sqlite3.so.3.2.3, and so on.
In order to use SOCI in your program, you need to specify your project build configuration with paths to SOCI headers and libraries, and specify linker to link against the libraries you

阅读全文
1 0
原创粉丝点击