有了oracle環境后,開始在Linux下編寫proc*c 初始例子一般都是一個連接到數據庫的程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlca.h"
EXEC SQL BEGIN DECLARE SECTION;
char *uid = "scott/tiger@CENTOS";
EXEC SQL END DECLARE SECTION;
int main()
{
EXEC SQL CONNECT :uid;
printf("%s",sqlca.sqlerrm.sqlerrmc);
if(sqlca.sqlcode == 0)
printf("Success!!!\n");
else
printf("Fail!!!\n");
}
將其保存為.pc的源程序文件。
流程為 (.pc源文件——>使用proc預編譯為.c文件——>使用gcc編譯連接——>生成可執行文件)。
使用proc預編譯的時候需要注意以下幾點 :
1、要正確的配置/u01/app/oracle/product/10.2.0/db_1/precomp/admin/pcscfg.cfg這個文件中的sys_include=(/u01/app/oracle/product/10.2.0/db_1/precomp/public,/usr/include,/usr/lib/gcc/i386-redhat-linux/4.1.1/include,/usr/lib/gcc/i386-redhat-linux/3.4.6/include)。查看本機的所擁有的庫文件:find /usr/lib -name "stddef.h" -print.修改相應的配置。
若沒有配置正確會產生一個錯誤信息:PCC-S-02015, unable to open include file
下面是比較全面的解析(E文的)
Cause: The precompiler could not open the listing file. This message can result from many causes. For example:
A pathname for a specified listing file contains a non-existent directory.
An operating-system error occurred because the file system or disk is full.
Write permission on the specified directory has not been granted.
Action: Track down the cause of the error, as suggested above, and correct it.
2、如果配置好后出現這個錯誤:PCC-F-02081, CMD-LINE: Unterminated option value list or value list was truncat ed.
Cause: An option that takes a list of values was entered. The value list did not have a closing parenthesis. This error may also occur if the list of values entered on a single line was too long and Pro*C truncated it.
Action: Ensure that all value lists are terminated with a closing parenthesis. Split long value lists into individual entries.
不要在一個include中寫入太長的內容,太多的話Pro*C會把它截斷的。可以分開寫或者確認其長度在適當的值內 。
最后使用gcc命令將其生成 gcc -o example example.c -I /u01/app/oracle/product/10.2.0/db_1/precomp/public/ -L /u01/app/oracle/product/10.2.0/db_1/lib/ -l clntsh
當然可以將該命令寫入一個文件中執行。