[轉]Linux下g++編譯C++連接oracle(OCCI)出現的問題及解決方式
由于項目原因,開始學習C++,剛接觸半個多月,今天參考網上例子,寫了個簡單的C++連接ORACLE的DEMO,可是使用g++編譯時不順利,不是報這個錯就是那個,最后參考網上的解決方式和個人理解,終于調試好了,現把編譯中出現的問題和解決方法總結出來。源代碼
C++代碼
- #include <iostream>
- #include <string>
- #include "occi.h"
- using namespace oracle::occi;
- using namespace std;
- int main()
- {
- string usr="sys";
- string pwd="orcl";
- string SID="ORCL";
- string date;
- Environment *env=Environment::createEnvironment(Environment::OBJECT);
- Connection *conn= env->createConnection(usr,pwd,SID);//all strings
- if(conn)
- cout<<"success createConnection!"<<endl;
- else
- cout<<"failure createConnection!"<<endl;
- Statement *stmt = conn->createStatement();
- string sSQL = "select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual";
- stmt->setSQL(sSQL);
- ResultSet *rs = stmt->executeQuery();
- if(rs->next())
- {
- date = rs->getString(1);
- }
- cout<<"now time :"<<date<<endl;
- env->terminateConnection(conn);
- Environment::terminateEnvironment(env);
- return 0;
- }
- #include <iostream>
- #include <string>
- #include "occi.h"
- using namespace oracle::occi;
- using namespace std;
- int main()
- {
- string usr="sys";
- string pwd="orcl";
- string SID="ORCL";
- string date;
- Environment *env=Environment::createEnvironment(Environment::OBJECT);
- Connection *conn= env->createConnection(usr,pwd,SID);//all strings
- if(conn)
- cout<<"success createConnection!"<<endl;
- else
- cout<<"failure createConnection!"<<endl;
- Statement *stmt = conn->createStatement();
- string sSQL = "select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual";
- stmt->setSQL(sSQL);
- ResultSet *rs = stmt->executeQuery();
- if(rs->next())
- {
- date = rs->getString(1);
- }
- cout<<"now time :"<<date<<endl;
- env->terminateConnection(conn);
- Environment::terminateEnvironment(env);
- return 0;
- }
本人linux上安裝oracle路徑:/opt/app/oracle/product/10.2.0/db_1
編譯命令:g++ -o conn -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -g
問題一:編譯時報如下錯誤:
Shell代碼
- [oracle@localhost demo]$ g++ g++ -o conn -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib -lclntsh -locci /usr/lib/libstdc++.so.5 conn_db.cpp -g
- g++: g++: No such file or directory
- conn_db.cpp:3:18: error: occi.h: No such file or directory
- conn_db.cpp:4: error: 'oracle' has not been declared
- conn_db.cpp:4: error: 'occi' is not a namespace-name
- conn_db.cpp:4: error: expected namespace-name before ';' token
- conn_db.cpp: In function 'int main()':
- conn_db.cpp:14: error: 'Environment' was not declared in this scope
- conn_db.cpp:14: error: 'env' was not declared in this scope
- conn_db.cpp:14: error: 'Environment' is not a class or namespace
- conn_db.cpp:14: error: 'Environment' is not a class or namespace
- conn_db.cpp:15: error: 'Connection' was not declared in this scope
- conn_db.cpp:15: error: 'conn' was not declared in this scope
- conn_db.cpp:21: error: 'Statement' was not declared in this scope
- conn_db.cpp:21: error: 'stmt' was not declared in this scope
- conn_db.cpp:26: error: 'ResultSet' was not declared in this scope
- conn_db.cpp:26: error: 'rs' was not declared in this scope
- conn_db.cpp:35: error: 'Environment' is not a class or namespace
解決:編譯時沒有引入OCCI頭文件,如果沒有,先下載對應的 ORACLE client安裝,比如我的是oracle10g,下載了oracle-instantclient-basic- 10.2.0.4-1.i386.zip,解壓到一個目錄下(/home/oracle/oracle/include),然后在編譯文件的時候引進這個解壓目錄
編譯命令增加OCCI目錄:g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -g
問題2:找不到對應函數
Shell代碼
- [oracle@localhost demo]$ g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -Wall -O -g
- /tmp/cclFs9xq.o: In function `main':
- /home/oracle/oracle/demo/conn_db.cpp:14: undefined reference to `oracle::occi::Environment::createEnvironment(oracle::occi::Environment::Mode, void*, void* (*)(void*, unsigned int), void* (*)(void*, void*, unsigned int), void (*)(void*, void*))'
- /home/oracle/oracle/demo/conn_db.cpp:35: undefined reference to `oracle::occi::Environment::terminateEnvironment(oracle::occi::Environment*)'
- collect2: ld returned 1 exit status
解決:增加libocci.so和libclntsh.so指定編譯
修改后的編譯命令: g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -lclntsh -locci -Wall -O -g
另外可能在引入-lclntsh -locci編譯時可能會報找不到以下錯誤:
Shell代碼
- [oracle@localhost demo]$ g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -lclntsh -locci /usr/lib/libstdc++.so.5 -Wall -O -g
- /usr/bin/ld: cannot find -lclntsh
- collect2: ld returned 1 exit status
- [oracle@localhost demo]$
解決:這是因為沒有找到libclntsh.so和libocci.so鏈接庫,你在可以把oracle client安裝目錄下把這兩個文件拷貝到$ORACLE_HOME/lib目錄下,或加到/usr/lib目錄下就可以了
問題三:occi在linux編譯運行時報libstdc++.so.6沖突的問題
Java代碼
- [oracle@localhost demo]$ g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -lclntsh -locci -Wall -O -g
- /usr/bin/ld: warning: libstdc++.so.5, needed by /opt/app/oracle/product/10.2.0/db_1/lib/libocci.so, may conflict with libstdc++.so.6
- [oracle@localhost demo]$ g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -lclntsh -locci -Wall -O -g
- /usr/bin/ld: warning: libstdc++.so.5, needed by /opt/app/oracle/product/10.2.0/db_1/lib/libocci.so, may conflict with libstdc++.so.6
解決:OCCI庫在linux編譯的時候,由于linux版本太高,會提示以上情況,實際上,在大多數linux系統上,還保留有libstdc++5的庫,自己手工在編譯的時候加上去就好了
修改后的編譯命令:g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib -lclntsh -locci /usr/lib/libstdc++.so.5 conn_db.cpp -g
編譯通過后執行結果輸出:
Shell代碼
- [oracle@localhost demo]$ g++ -o conn -I/home/oracle/oracle/include -L/opt/app/oracle/product/10.2.0/db_1/lib -L/opt/oracle/product/10.2.0/db_1/rdbms/lib conn_db.cpp -lclntsh -locci /usr/lib/libstdc++.so.5 -Wall -O -g
- [oracle@localhost demo]$ ./conn
- success createConnection!
- now time :2010-11-14 22:49:24
- [oracle@localhost demo]$