oracledba.help
QuickStarts

C

<- QuickStarts

Overview

This Quickstart covers using GCC with OCILIB to create applications that can use Oracle. According to the TIOBE Index C is and continues to be the most used computer language. The GNU Compiler Collection (GCC) was developed to be 100% free software, free in the sense that it respects the user's freedom. This includes not being bound to any particular proprietary constraints and limitations.

TDM-GCC is a compiler suite for Windows that combines the most recent stable release of the GCC toolset and the open-source MinGW runtime APIs. It can create 32-bit or 64-bit binaries for any version of Windows. OCILIB is an open source Oracle driver that delivers efficient access to Oracle databases. OCILIB may very well be the easiest way to connect to Oracle using C or C++. This Quickstart example focuses on creating 32-bit Windows applications to connect to a modern an Oracle database.

Prerequisites

Other Useful Links

Install Oracle InstantClient

  1. Unset your ORACLE_HOME variable.
  2. Run setup.exe for InstantClient.
  3. (x) InstantClient Software
  4. Software Location
    Path: c:\app\oracle\product\12.1.0.1\ic32
  5. Install
  6. Close
  7. Reset your ORACLE_HOME variable.
 Note: This will change your PATH as so: c:\app\oracle\product\12.1.0.1\ic32;PATH
       Which in turn enables the path to the Oracle InstantClient oci.dll.

GCC Installation

  1. Run setup.exe
  2. If you do not have Internet access:
    [ ] Disable the check for updated files on the TDM-GCC server
  3. Select the Create button.
  4. Accept License by selecting Next button.
  5. Use the default location: C:\TDM-GCC-32 by selecting Next button.
  6. Use the default components by selecting the Install button.
    Note: The ...\bin directory will be added to the PATH variable.
  7. Once Completed Successfully select the Next button.
  8. Select the Finish button.
    Reading the README file can be useful. ;-)

Set Environment

  • Ensure the following is in your PATH variable: C:\TDM-GCC-32\bin;
    Note this is all the provided MinGW Command Prompt (mingwvars.bat) does.
  • Create the environment variable C_INCLUDE_PATH to C:\ocilib\include;
  • Create the environment variable LIBRARY_PATH to C:\ocilib\lib32;

Test GCC

  1. Open a the provided MinGW Command Prompt.
    This runs the mingwvars.bat to set your environment.
  2. Run the following command: gcc --version
  3. You should see the gcc version and a small banner.

Create a Test GCC Application

  1. Go to your C:\app\gcc directory.
  2. Create a file with the contents shown below as hello.c.
  3. Compile it using this command line: gcc -Wall -o hello.exe hello.c
  4. Run it by entering: hello
  5. You should see the famous Hello World! message displayed.
#include "stdio.h"

int main() {
   printf("Hello World!\n");
   return 0;
}

Test Windows Environment

  1. Create a file with the contents shown below as: helloWin.c
  2. Compile the below using the command line: gcc -Wall -o helloWin.exe helloWin.c
  3. Run it by entering: helloWin
  4. You should see a message box window displayed.
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow) {

    MessageBox(NULL, "Hello Windows!", "Note", MB_OK|MB_ICONINFORMATION);
    return 0;
}

OCILIB Installation and Test

  1. Unzip the ocilib-ver-windows.zip to C:\ocilib
  2. Create a file with the contents shown below as oratest.c entering your Oracle connection values where indicated.
  3. Compile it using this command line:
    gcc -Wall -o oratest.exe oratest.c -L. -lociliba -DOCI_CHARSET_ANSI -DOCI_API=__stdcall
  4. Run it by entering: oratest
  5. You should see the the date displayed.
#include "ocilib.h"
#include "stdio.h"

void err_handler(OCI_Error *err) {
  printf(
     "code: ORA-%05i\n"
     "msg:  %s\n"
     "sql:  %s\n",
     OCI_ErrorGetOCICode(err),
     OCI_ErrorGetString(err),
     OCI_GetSql(OCI_ErrorGetStatement(err))
     );
}

int main(int argc, char *argv[]) {
    OCI_Connection *cn;
    OCI_Statement  *st;
    OCI_Resultset  *rs;	

    printf("OCI_Initialize()\n");		
    if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT)) return EXIT_FAILURE;

    cn = OCI_ConnectionCreate("InstanceName", "UserName", "Password", OCI_SESSION_DEFAULT);
    printf("OCI_ConnectionCreate()\n");	
    if (cn == NULL) {
      OCI_Error *err = OCI_GetLastError();
      printf("errcode %d, errmsg %s", OCI_ErrorGetOCICode(err), OCI_ErrorGetString(err) );	
    }

    printf("OCI_StatementCreate()\n");
    st  = OCI_StatementCreate(cn);

    printf("OCI_ExecuteStmt()\n");
    OCI_ExecuteStmt(st, "SELECT sysdate FROM dual");

    printf("OCI_GetResultset()\n");
    rs = OCI_GetResultset(st);

    printf("OCI_FetchNext()\n");
    while (OCI_FetchNext(rs)) {
       printf("code: %i, name %s\n", OCI_GetInt(rs, 1)  , OCI_GetString(rs, 1));
       printf("\n%d row(s) fetched\n", OCI_GetRowCount(rs));
    }

    OCI_Cleanup();	
    return EXIT_SUCCESS;
}

Compile and Run Within Notepad++

-- Create Run Item
Press F6 then paste the below.

NPP_SAVE
ENV_SET PATH = $(SYS.PATH);C:\app\oracle\product\12.1.0.1\ic32;
ENV_SET C_INCLUDE_PATH = C:\ocilib\include;
ENV_SET LIBRARY_PATH = C:\ocilib\lib32;

cmd /c del $(CURRENT_DIRECTORY)\$(NAME_PART).exe
cd "$(CURRENT_DIRECTORY)"
gcc -Wall -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe $(CURRENT_DIRECTORY)\$(NAME_PART).c 
    -L. -lociliba -DOCI_CHARSET_ANSI -DOCI_API=__stdcall
$(NAME_PART).exe

Save as: GCC Run

Press F6 then paste the below.

NPP_SAVE
ENV_SET PATH = $(SYS.PATH);C:\app\oracle\product\12.1.0.1\ic32;
ENV_SET C_INCLUDE_PATH =  C:\TDM-GCC-32\include;C:\ocilib\include;
ENV_SET LIBRARY_PATH =C:\TDM-GCC-32\lib;C:\ocilib\lib32;

echo $(SYS.PATH)
echo $(SYS.C_INCLUDE_PATH)
echo $(SYS.LIBRARY_PATH)

cmd /c del $(CURRENT_DIRECTORY)\$(NAME_PART).exe
cd "$(CURRENT_DIRECTORY)"
windres -o $(CURRENT_DIRECTORY)\$(NAME_PART).o $(CURRENT_DIRECTORY)\$(NAME_PART).rc
gcc -Wall -mwindows -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe $(CURRENT_DIRECTORY)\$(NAME_PART).o 
     $(CURRENT_DIRECTORY)\$(NAME_PART).c 
    -L. -lociliba -DOCI_CHARSET_ANSI -DOCI_API=__stdcall
cmd /c $(CURRENT_DIRECTORY)\$(NAME_PART).exe

Save as: GCC Run Win Res

-- Create NppExec Item
Go to Advance Options within NppExec plugin,

 A. Check the box at the top that says "Place to the Macros Submenu"
 B. Select script from "Associated Script" combo box. It will automatically fill in the "Item Name".
 C. Now click the "Add/Modify" button.
 D. Click OK. This will exit the Advanced Options box and say that NotePad++ needs to be restarted.

-- Assign to HotKey

 A. Navigate to: Settings -> Shortcut Mapper -> Plugin Commands -> GCC Run
 B. Right-Click -> Modify
 C. Select Ctrl-F1 (or other combo you desire)

<- QuickStarts