OCILIB Tutorials v5.0.0
Beginner

Your first OCILIB project

Objectives — by the end of this page you will be able to:

The skeleton

Every program in these tutorials has the same shape:

#include "ocilib.h"
#include <stdio.h>
#include <stdlib.h>

void err_handler(OCI_Error *err)
{
    printf("%s\n", OCI_ErrorGetString(err));
}

int main(void)
{
    OCI_Environment *env = OCI_EnvironmentCreate(err_handler, NULL, OCI_ENV_DEFAULT);
    if (!env)
    {
        return EXIT_FAILURE;
    }

    /* … your database work here … */

    OCI_EnvironmentFree(env);
    return EXIT_SUCCESS;
}

One header per language — #include "ocilib.h" for C, "ocilib.hpp" for C++ (the C++ header is self-contained and pulls in the C header for you).

Assume OCILIB installed in /usr/local (Linux) or built in lib64\ with headers in include\ (Windows):

# Linux / macOS — C
gcc  -std=c11   app.c   $(pkg-config --cflags --libs ocilib) -o app
# C++
g++  -std=c++17 app.cpp $(pkg-config --cflags --libs ocilib) -o app

Strings the OCILIB way: otext and OTEXT()

Because the same code must compile against MBCS (char) and wide (wchar_t) builds, OCILIB never uses plain string literals for API calls:

OCI_StatementExecute(st, OTEXT("select ename from employees"));   /* portable */
OCI_StatementExecute(st, "select ename from employees");          /* MBCS build only */

Plain const char* literals happen to compile on MBCS builds, and C++ samples accept const char* too — but get in the OTEXT() habit early; it is free and portable.

Try it yourself

  1. Copy the C skeleton into app.c, compile, run — it should exit silently with code 0.
  2. Deliberately remove the Instant Client from PATH/LD_LIBRARY_PATH and run again: OCI_EnvironmentCreate returns NULL / throws — that is your error channel working.
  3. Add -Wall -Wextra (gcc/clang) or /W4 (MSVC) — the OCILIB headers are warning-clean.

Common pitfalls

Recap

Next: make the environment actually do something — error handlers and env flags.