Home » Developer & Programmer » Precompilers, OCI & OCCI » Not able to compile simple OCI program on linux
Not able to compile simple OCI program on linux [message #584610] Thu, 16 May 2013 03:46 Go to next message
Messages: 56
Registered: August 2011

I am trying to run a sample test program on Linux wherein it makes OCI calls as below,

#include <stdio.h>
#include <string.h>
#include "oci.h"
#include "ociapr.h"

void main()
   *  Handles.
  OCIEnv     *envhp;                    /* Environment handle           */
  OCIError   *errhp;                    /* Error handle                 */
  OCIServer  *srvhp;                    /* Server handle                */
  OCISvcCtx  *svchp;                    /* Service context handle       */
  OCISession *usrhp;                    /* Session handle               */

  OCIInitialize((ub4)OCI_DEFAULT,(dvoid *)0,(dvoid * (*)())0,
                (dvoid * (*)())0,(void (*)())0);

  OCIEnvInit((OCIEnv **)&envhp,(ub4)OCI_DEFAULT,(size_t)0,(dvoid **)0);

  OCIHandleAlloc((dvoid *)envhp,(dvoid **)&errhp,(ub4)OCI_HTYPE_ERROR,
                 (size_t)0,(dvoid **)0);

  OCIHandleAlloc((dvoid *)envhp,(dvoid **)&srvhp,(ub4)OCI_HTYPE_SERVER,
                 (size_t)0,(dvoid **)0);

  OCIServerAttach((OCIServer *)srvhp,(OCIError *)errhp,
                                 (text *)0,(sb4)0,(ub4)OCI_DEFAULT);

  OCIHandleAlloc((dvoid *)envhp,(dvoid **)&svchp,(ub4)OCI_HTYPE_SVCCTX,
                 (size_t)0,(dvoid **)0);

  OCIAttrSet((dvoid *)svchp,(ub4)OCI_HTYPE_SVCCTX,
             (dvoid *)srvhp,(ub4)0,
             (ub4)OCI_ATTR_SERVER,(OCIError *)errhp);

  OCIHandleAlloc((dvoid *)envhp,(dvoid **)&usrhp,(ub4)OCI_HTYPE_SESSION,
                 (size_t)0,(dvoid **)0);

  OCIAttrSet((dvoid *)usrhp,(ub4)OCI_HTYPE_SESSION,
             (dvoid *)"scott",(ub4)strlen("scott"),
             (ub4)OCI_ATTR_USERNAME,(OCIError *)errhp);

  OCIAttrSet((dvoid *)usrhp,(ub4)OCI_HTYPE_SESSION,
             (dvoid *)"tiger",(ub4)strlen("tiger"),
             (ub4)OCI_ATTR_PASSWORD,(OCIError *)errhp);

  OCISessionBegin((OCISvcCtx *)svchp,(OCIError *)errhp,
                                 (OCISession *)usrhp,

  OCIAttrSet((dvoid *)svchp,(ub4)OCI_HTYPE_SVCCTX,
             (dvoid *)usrhp,(ub4)0,
             (ub4)OCI_ATTR_SESSION,(OCIError *)errhp);

  OCISessionEnd((OCISvcCtx *)svchp,(OCIError *)errhp,
                               (OCISession *)usrhp,(ub4)OCI_DEFAULT);

 OCIServerDetach((OCIServer *)srvhp,(OCIError *)errhp,

  OCIHandleFree((dvoid *)srvhp,(ub4)OCI_HTYPE_SERVER);
  OCIHandleFree((dvoid *)svchp,(ub4)OCI_HTYPE_SVCCTX);
  OCIHandleFree((dvoid *)usrhp,(ub4)OCI_HTYPE_SESSION);
  OCIHandleFree((dvoid *)errhp,(ub4)OCI_HTYPE_ERROR);
  OCIHandleFree((dvoid *)envhp,(ub4)OCI_HTYPE_ENV);

I edited the demo_rdbms.mk file and it looks like,
DEMO_MAKEFILE = $(DEMO_DIR)/demo_rdbms.mk


ocitest: test_oci.o
         gcc test_oci.o -o ocitest

test_oci.o: test_oci.c
         gcc -Wall -c -I/u01/app/oracle/product/11.2.0/db_1/rdbms/lib -I/u01/app/oracle/product/11.2.0/db_1/rdbms/public -L$ORACLE_HOME/lib -lclntsh test_oci.c

When I issue make as below I get errors


make -f demo_rdbms.mk ocitest
gcc test_oci.o -o ocitest
test_oci.o: In function `main':
test_oci.c:(.text+0x14e): undefined reference to `OCIInitialize'
test_oci.c:(.text+0x169): undefined reference to `OCIEnvInit'
test_oci.c:(.text+0x1b5): undefined reference to `OCIHandleAlloc'
test_oci.c:(.text+0x1d8): undefined reference to `OCIHandleAlloc'
test_oci.c:(.text+0x224): undefined reference to `OCIServerAttach'
test_oci.c:(.text+0x285): undefined reference to `OCIHandleAlloc'
test_oci.c:(.text+0x2ac): undefined reference to `OCIAttrSet'
test_oci.c:(.text+0x2cf): undefined reference to `OCIHandleAlloc'
test_oci.c:(.text+0x2f7): undefined reference to `OCIAttrSet'
test_oci.c:(.text+0x31f): undefined reference to `OCIAttrSet'
test_oci.c:(.text+0x341): undefined reference to `OCISessionBegin'
test_oci.c:(.text+0x3a6): undefined reference to `OCIAttrSet'
test_oci.c:(.text+0x3c2): undefined reference to `OCISessionEnd'
test_oci.c:(.text+0x418): undefined reference to `OCIServerDetach'
test_oci.c:(.text+0x467): undefined reference to `OCIHandleFree'
test_oci.c:(.text+0x478): undefined reference to `OCIHandleFree'
test_oci.c:(.text+0x489): undefined reference to `OCIHandleFree'
test_oci.c:(.text+0x49a): undefined reference to `OCIHandleFree'
test_oci.c:(.text+0x4ab): undefined reference to `OCIHandleFree'
collect2: ld returned 1 exit status
make: *** [ocitest] Error 1

Please advice.

Thanks n Regards,
Re: Not able to compile simple OCI program on linux [message #678700 is a reply to message #584610] Fri, 27 December 2019 13:02 Go to previous message
Messages: 25
Registered: November 2017
Junior Member
Your Makefile is a bit inconsistent. A possible fix:

DEMO_MAKEFILE = $(DEMO_DIR)/demo_rdbms.mk


test_oci: test_oci.o
	  gcc -L${ORACLE_HOME}/lib -lclntsh -o $@ $^

test_oci.o: test_oci.c
	 gcc -Wall -c -I$(ORACLE_HOME)/rdbms/public -o $@ $^
Previous Topic: sql_context
Next Topic: In Pro*C, can UPDATE return sqlca.sqlcode=1403 ?
Goto Forum:

Current Time: Mon Feb 26 01:15:08 CST 2024