Oracle数据库开发(三).Pro*C/C++的编译参数

Oracle Database Development (3). Introduce to Pro*C/C++ Precompiler Options

Vert Melon

Jun 7,2007


1.Preface

 Now we have already known about the Pro*C/C++ Development Environment in Windows
 or Linux, but it is not enough.
 
 Anyway, esaier said than done, let's start our trip.


2.What is the Precompiler

 During precompilation, Pro*C/C++ generates C or C++ code that replaces the SQL
 statements embedded in your host program. The generated code contains data structures
  that indicate the datatype, length, and address of host variables, as well as other
 information required by the runtime library, SQLLIB. The generated code also contains
 the calls to SQLLIB routines that perform the embedded SQL operations.
 The precompiler does not generate calls to Oracle Call Interface (OCI) routines.
 
 The paragraph above is copied from 《Pro*C/C++ Precompiler Programmer's Guide》. I'm
  clever at doing this ^_^ .
 
  SQLLIB named "orasql9.lib" in Windows, but named "libclntsh.so.9.0" in Linux.
  You could get more messages if your find it in the last articles within this series.
   Oracle Database Development  (1). Configuration of ProC in Windows
   Oracle Database Development  (2). Configuration of ProC in Linux
 
3.Use the  Precompiler Options
 
  Precompiler options enable you to control how resources are used, how errors are reported,
  how input and output are formatted, and how cursors are managed.
 
  In the Windows , most people prefer to use "procui.exe" to precompile host program.
  Of course , the tool provides "Options" menu which gives us a platform to edit options.
  When we use Linux OS, we can appoint the parameters in the command line. For example :
  $ proc parse=none iname=main.pc
 
  Developer can get more information by typing command " proc ? " in both Windows and Linux.
 
  The method mentioned above is only one of the Using Precompiler Option, which i can
  call it "command line" .
 
  In Oralce, there are many methods like this , such as :
 
  system configuration file
  user configuration file
  command line
  inline
 
  There is a single system configuration file for each installation. The name of the
  system configuration file is pcscfg.cfg. The directory of the file is "$ORACLE_HOME/precomp/admin" .
 
  We can specifies the name of a user configuration file by using the CONFIG option.
 
  "inline" means that the declaration is in the host program. This statements begin
  with "EXEC SQL" ,and not every option can be declared as "inline".

  The precedence of option values is explained indisputably in Oracle referential document.
  I don't want to waste so much time talking about the usage because of the 《Pro*C/C++ Precompiler Programmer's Guide》.
  ORAC
  Luckily, i am not so stupid to forget my original purpose. Here comes the key .
 

4.The Important Options

  CONFIG , PARSE and SYS_INCLUDE
 
  Type the command "proc ?" and take a overview of the description , you will find
  that the default value of CONFIG is null. Oracle always use the system configuration
  file except CONFIG is defined .
 
  In Linux OS environment , the default file named "pcscfg.cfg" includes the option
  "SYS_INCLUDE" . But if PARSE=NONE  , this option is irrelevant for the precompilation.
  That is the reason you can use "proc parse=none iname=main.pc" precompile the host
  program so easily.
 
  When PARSE=FULL , your must define the value of SYS_INCLUDE , and then you can
   declare host variables legally in C.
 
 #include "sqlca.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 void sql_error(char *msg)
 {
  printf("/n%s %s/n", msg,(char *)sqlca.sqlerrm.sqlerrmc);
  //EXEC SQL ROLLBACK RELEASE;
  exit(0);
 }
 
 void main() {
 
 EXEC SQL INCLUDE sqlca;
 EXEC ORACLE OPTION (RELEASE_CURSOR = YES);
 EXEC SQL WHENEVER SQLERROR DO sql_error(" <ERROR> ");
 
 char oraCN[200];
 strcpy(oraCN, "system/ddd@unixdb");
 
 EXEC SQL CONNECT :oraCN;
 printf("/n [OK Connected!] ");
 
 }

 
  In Windows OS , maybe the value is "E:/Program Files/Microsoft Visual Studio/VC98/Include" .
  Edit the "pcscfg.cfg" file and precomplie the code above . The programe runs
   very good.
  
  The case becomes a litter complex in Linux .
 
 $ pwd
 /home/ora/ora9/oracle/precomp/admin
 $ cat pcscfg.cfg
 sys_include=(/usr/include,/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include)
 ltype=short
 
  $ proc parse=full iname=main.pc
  $ gcc -g -o main main.c -I/home/ora/ora9/oracle/precomp/public -L/home/ora/ora9/oracle/lib -lclntsh

  It runs very good too .
 
 
  ------------------------------------------------------
  Oracle refrence
 
 PARSE=NONE
   The value NONE has the following effects:
  C preprocessor directives are understood only inside a declare section.
  You must declare all host variables inside a Declare Section.
  Precompiler release 1.x behavior
 
 PARSE=PARTIAL
   The value PARTIAL has the following effects:
  All preprocessor directives are understood
  You must declare all host variables inside a Declare Section
  This option value is the default if CODE=CPP
 
 PARSE=FULL
  The value FULL has the following effects:
  The precompiler C parser runs on your code.
  All Preprocessor directives are understood.
  You can declare host variables at any place that they can be declared legally in C. 

5.Postscript
 
  When I used the option "PARSE=FULL" , i found that "EXEC SQL INCLUDE sqlca;"
  "EXEC SQL ROLLBACK RELEASE; " cannot be precompled correctly together.
  I have no idea about it . Perhaps it caused by the internal compatibility of SQLCA.
 
  I am sure i will work along with it.

 

 


Oracle数据库开发(三).ProC的预编译参数

草木瓜

20070607 

一、前言

 我们上面已经了解Windows和Linux下的ProC开发环境,这里我们更进一步去
简要介绍下ProC的预编译参数。

二、什么是预编译

 预编译过程中,Pro*C/C++会自动生成C或者C++的代码,去替代你原来的嵌入SQL,
生成的代码包含了一些数据结构,其中声明了数据类型,长度,变量地址以及SQLLIB需
要的一些其他信息。生成的代码还包括了执行嵌入SQL操作的一些SQLLIB调用。

  SQLLIB Windows下名是 "orasql9.lib" ,Linux下是"libclntsh.so.9.0" 。这方面知识可以参
 考先前的文章:
   《Oracle数据库开发(一).Windows下配置使用ProC》
   《Oracle数据库开发(二).Linux下配置使用ProC》
 
三、使用预编译选项

 预编译选项可以控制编译资源的使用,错误提示,标准化输入输出以及管理游标。
 在Windows下,多数人倾向于使用“procui.exe”(Oralce菜单里的ProC*C++工具)进行
预编译,可以通过Options菜单选择编译选项。当我们使用Linux平台时,一般会在命
令行下指定参数,例如:
  $ proc parse=none iname=main.pc
  
  不管是Windows还是Linux,开发人员可以都键入“proc ?”获取更多的信息。
 
  在Oracle中,关于参数的使用有不少的方法,如:
 
  系统参数文件
  用户参数文件
  命令行
  inline
 
  ProC安装后都会存在唯一的系统参数文件“pcscfg.cfg”,所在路径为“$ORACLE_HOME/precomp/admin”。
用户的参数文件,我们可以手工指定。命令行很容易理解,上面已有相关说明。“inline”
的意思是在原始pc文件中通过“EXEC SQL”显式声明,不过不是所有的参数可以使用
这种方式声明。
 
 关于这些优先级的说明,在Oracle相关的官方文档已经说得很明确。具体内容可
参看《Pro*C/C++开发指南》,这里不再赘述。

四、一些比较重要的参数

 CONFIG,PARSE 和 SYS_INCLUDE
 
 在命令行下输入“proc ?”,简单浏览下显示信息,你会发现CONFIG的默认值是空。
ORACLE一直使用的是系统的参数文件,除非你手工去指定config选项。
 在Linux环境下,默认的系统参数文件“pcscfg.cfg”中含有“SYS_INCLUDE”的选项,
但是如果“PARSE=NONE",这个“SYS_INCLUDE”的选项会被忽略。所以你可以使用
“proc parse=none iname=main.pc”(上篇文章)轻松编译。

 不过当我们指定PARSE值为FULL时,你就必须正确定义“SYS_INCLUDE”,这时可
以在ProC的源文件.pc中,直接使用C语法规则定义宿主变量。可以看下面的例子:


 #include "sqlca.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 void sql_error(char *msg)
 {
  printf("/n%s %s/n", msg,(char *)sqlca.sqlerrm.sqlerrmc);
  //EXEC SQL ROLLBACK RELEASE;
  exit(0);
 }
 
 void main() {
 
 EXEC SQL INCLUDE sqlca;
 EXEC ORACLE OPTION (RELEASE_CURSOR = YES);
 EXEC SQL WHENEVER SQLERROR DO sql_error(" <ERROR> ");
 
 char oraCN[200];
 strcpy(oraCN, "system/ddd@unixdb");
 
 EXEC SQL CONNECT :oraCN;
 printf("/n [OK Connected!] ");
 
 }
 
 在Windows下,“SYS_INCLUDE"的值类似于“E:/Program Files/Microsoft Visual Studio/VC98/Include”,
即Vc6.0的头文件目录,编辑“pcscfg.cfg”,然后预编译源代码,程序运行没有任何问题。

 在Linux下,可能要稍微复杂一些。
 
 $ pwd
 /home/ora/ora9/oracle/precomp/admin
 $ cat pcscfg.cfg
 sys_include=(/usr/include,/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include)
 ltype=short
 
    $ proc parse=full iname=main.pc
    $ gcc -g -o main main.c -I/home/ora/ora9/oracle/precomp/public -L/home/ora/ora9/oracle/lib -lclntsh
   
    依上编译,也没有问题。
   
    ------------------------------------------------------
    Oracle 参考
   
      PARSE=NONE
       所有宿主变量必须定义在声明段中。只有在声明段中的预编译指令才会
       识别。
      PARSE=PARTIAL
       可以识别所有的预编译指令,所有宿主变量必须定义在声明段中。如果
       CODE=CPP,这个是默认选项。
      PARSE=FULL
       可以识别所有的预编译指令,可以直接使用C语法定义宿主变量,不必
       定义在声明段中。
  
五、后记

 在我使用“PARSE=FULL”选项时,发现“EXEC SQL INCLUDE sqlca;” 和 “EXEC SQL ROLLBACK RELEASE; ”
不能同时使用。不知道是为什么,估计可能是SQLCA的内部兼容性问题。相信有时
间能解决这个问题。

 

你可能感兴趣的:(Oracle数据库开发(三).Pro*C/C++的编译参数)