码迷,mamicode.com
首页 > 其他好文 > 详细

初探oci之编译问题小结

时间:2015-01-30 17:45:27      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:occi   oracle   

〇、前言

继续上篇文章的讲述,让我们一块走进神奇的oci。

一、oci的概念

ORACLE调用接口(Oracle Call Interface简称OCI)提供了一组可对ORACLE数据库进行存取的接口子例程(函数),通过在第三代程序设计语言(如C语言)中进行调用可达到存取ORACLE数据库的目的。

在普通的情况下,用户可以通过SQL和PL/SQL来访问数据库中的数据。ORACLE调用接口ORACLE数据库除了提供SQL和PL/SQL来访问数据库外,还提供了一个第三代程序设计语言的接口,用户可以通过C、COBOL、FORTRAN等第三代语言来编程访问数据库。OCI就是为了实现高级语言访问数据库而提供的接口。OCI允许开发者在程序里使用SQL和PL/SQL来访问数据库。开发者可以使用第三代语言来编写程序,而使用OCI来访问数据库。

OCI是由一组应用程序开发接口(API)组成的,ORACLE提供API的方式是提供一组库。这组库包含一系列的函数调用。这组函数包含了连接数据库、调用SQL和事务控制等。在安装DBMS SERVER或者客户端的时候,就安装了OCI。

二、oci demo开发

  • 在部署好的Oracle客户端环境中,/opt/oracle/sdk/demo下面有oci demo程序:
 occidemo.sql、occidml.cpp
  • 代码很简单,一目了然后:
  1. 修改数据库帐号、密码、数据库tns名,env->createConnection(user, passwd, db)
  2. 在数据库中创建table
  • 编译语句:
 g++ -I/opt/oracle/sdk/include -I/usr/local/include/boost -L/opt/oracle/lib -o ociTest occidml.cpp -lclntsh -locci /usr/lib/libstdc++.so.5
  • 下面开始编译和测试。

三、编译过程中碰到的问题

1、-lclntsh -locci

  • 问题:-lclntsh -locci
  • 报错:undefined reference to `oracle::occi::Environment::createEnvironment
  • 解决方案:-lclntsh这是连接oracle的动态库,没有这个就不能连接oracle了


2、cannot find -lclntsh

  • 问题:cannot find -lclntsh(-lclntsh -locci)
  • 报错:
[root@bogon cppTest]# g++ -I/opt/oracle/sdk/include -I/usr/local/include/boost -L/opt/oracle/lib -o ociTest occidml.cpp -lclntsh -locci /usr/lib/libstdc++.so.5
/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
  • 分析:
-lclntsh  指连接lib文件libclntsh.so
-locci    指连接lib文件libocci.so
Oracle客户端的lib包为:libclntsh.so.10.1、libocci.so.10.1
  • 解决方案:
cp libclntsh.so.10.1 libclntsh.so
cp libocci.so.10.1 libocci.so

3、Oracle与libstdc++.so

  • 问题:Oracle与libstdc++.so
  • 报错:libstdc++.so.5, needed by /opt/oracle/lib/libocci.so, not found

或者:warning: libstdc++.so.5, needed by /home/oracle/OraHome1/lib/libocci.so, may conflict with libstdc++.so.6

  • 分析:

Oracle10.2.0.4的会依赖与低版本的标准C++库libstdc++.so.5

  • 推荐的解决方案:
1)downgrade the oracle to 10.2.0.2.
2)Append the libstdc++.so.5 to package, and use command “ cd /usr/lib;      ln /opt/webex/imds/lib/so/libstdc++.so.5.0.7  libstdc++.so.5”

      By this way, when compiling, compiler will report : /usr/bin/ld: warning: libstdc++.so.5, needed by /home/oracle/OraHome1/lib/libocci.so, may conflict with libstdc++.so.6

       I am still not sure whether will affect the action of program. Need run for a period to test.

3)  Build all library which imds is using, include TP, WDMS, Jabber decrypt. It is very hard and painful.

4) Upgrade oracle to 11

个人认为,方案4会更好一些。

方案2在尝试中,根据调查,最好不要在一个程序中同时依赖 libstdc++.so.5和libstdc++.so.6。
  • 本人解决方案:
  1. 上传libstdc++.so.5到/usr/lib/下。
  2. 编译语句更改为:g++ -I/opt/oracle/sdk/include -I/usr/local/include/boost -L/opt/oracle/lib -o ociTest occidml.cpp -lclntsh -locci /usr/lib/libstdc++.so.5(/usr/lib/libstdc++.so.5,强制使用5版本的so文件)

http://www.itpub.net/thread-465101-1-1.html


4、g++和gcc的区别

  • 问题:g++和gcc的区别
  • 报错:
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::s
ize() const‘
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::
operator[](unsigned int) const‘
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::
operator[](unsigned int) const‘
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::
operator[](unsigned int) const‘
undefined reference to `std::cout‘
undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std
::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)‘
undefined reference to `std::ios_base::Init::Init()‘
undefined reference to `std::ios_base::Init::~Init()‘
collect2: ld returned 1 exit status
  • 解决方案:gcc HelloWorld.cpp -lstdc++ -o HelloWorld

g++ HelloWorld.cpp -o HelloWorld (用g++, 效果是一样的, stdc++会被自动连接)

5、cannot restore segment prot after reloc

http://hi.baidu.com/kzsoft/blog/item/a507ca182fbdf10135fa41a4.html http://www.cppblog.com/soak/

四、参考网址

初探oci之编译问题小结

标签:occi   oracle   

原文地址:http://blog.csdn.net/san1156/article/details/43308457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!