Beginner
Your first OCILIB project
Objectives — by the end of this page you will be able to:
- lay out a minimal C and C++ OCILIB application
- compile and link it with gcc, MSVC or CMake
- use
otext/OTEXT()so your source works on MBCS and wide builds
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;
}
#include "ocilib.hpp"
#include <iostream>
using namespace ocilib;
int main()
{
try
{
Environment env;
/* … your database work here … */
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
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).
Compile & link
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
# Windows (x64 MBCS build of ocilib)
cl /std:c17 /I include app.c /link lib64\ocilib.lib
cl /std:c++17 /I include app.cpp /link lib64\ocilib.lib
# CMakeLists.txt — works on both OSes
cmake_minimum_required(VERSION 3.20)
project(ocilib_app C CXX)
find_library(OCILIB_LIB NAMES ocilib ocilib
PATHS ${OCILIB_ROOT}/lib64 /usr/local/lib)
find_path(OCILIB_INC NAMES ocilib.h
PATHS ${OCILIB_ROOT}/include /usr/local/include)
add_executable(app app.c) # or app.cpp
target_include_directories(app PRIVATE ${OCILIB_INC})
target_link_libraries(app PRIVATE ${OCILIB_LIB})
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:
otext— the character type of the build (charorunsigned short)OTEXT("…")— a literal of the right kind, everywhere the API takes a stringocilib::String— C++ string type (std::stringorstd::wstring)
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
- Copy the C skeleton into
app.c, compile, run — it should exit silently with code 0. - Deliberately remove the Instant Client from
PATH/LD_LIBRARY_PATHand run again:OCI_EnvironmentCreatereturns NULL / throws — that is your error channel working. - Add
-Wall -Wextra(gcc/clang) or/W4(MSVC) — the OCILIB headers are warning-clean.
Common pitfalls
- Linking the wrong lib name on Windows —
ocilib.lib(MBCS) vsocilibw.lib(wide); they are not interchangeable. fatal error: ocilib.h: No such file— include dir missing; point at the repo'sinclude/or the installinclude/.- It builds but does nothing — correct! The skeleton only creates and frees the environment. The next page makes it talk to Oracle.
Recap
- One include, one env object, try/catch (C++) or NULL check (C).
pkg-config --cflags --libs ocilibon Linux;ocilib.lib/ocilibw.libon Windows.otext/OTEXT()/ocilib::Stringkeep your code charset-agnostic.
Next: make the environment actually do something — error handlers and env flags.