Skip to content

Instantly share code, notes, and snippets.

@bearx3f
Last active September 16, 2017 16:56
Show Gist options
  • Save bearx3f/8f60a1c431fb0623af59b2a1c6248786 to your computer and use it in GitHub Desktop.
Save bearx3f/8f60a1c431fb0623af59b2a1c6248786 to your computer and use it in GitHub Desktop.

Install go-oci8 on Windows 64

Extract

  • Extract instantclient into C:\oracle\instantclient_12_1
  • Install TDM-GCC into C:\TDM-GCC-64

Convert .dll -> .a

If you try to build without do this you'll face with "error adding symbols: File in wrong format". and i'm realized oci.lib is under sdk/lib/msvc directory and GCC toolchain doesn't regconize archive library that build by Visual Studio so we have to reformat it by using pexport

For most convinient I'm using Git Bash

cd /c/oracle/instantclient_12_1

Prepare directory

mkdir -pv sdk/lib/gcc

Create definition file from dynamic link library

pexports oci.dll > oci.def

Create archive file (.a) from created definition file

dlltools -D oci.dll -d oci.def -l sdk/lib/gcc/liboci.a

Now we have archive that gcc regconize.

Setup Environment Variable

Add C:\oracle\instantclient_12_1 into you Windows PATH

Create pkg-config file

fill C:\oracle\instantclient_12_1\oci8.pc with content below.

ora=C:/oracle/instantclient_12_1/sdk
gcc=C:/TDM-GCC-64

oralib=${ora}/lib/gcc
orainclude=${ora}/include

gcclib=${gcc}/lib
gccinclude=${gcc}/include

glib_genmarshal=glib-genmarshal
gobject_query=gobject-query
glib_mkenums=glib-mkenums

Name: oci8
Description: Oracle database engine
Version: 12.1
Libs: -L${oralib} -L${gcclib} -loci
Libs.private: 
Cflags: -I${orainclude} -I${gccinclude}

GO! Get go-oci8

And now ...

go get -v github.com/mattn/go-oci8

Visual Studio Code's task

tasks.json // for compile

In case you're using Visual Studio code here tasks.json for build

{
    "taskName": "build",
    "isBuildCommand": true,
    "echoCommand": true,
    "command": "go",
    "args": [
        "build",
        "-x",
        "-ldflags=-s -w",
        "src/main.go"
    ],
    "options": {
        "cwd": "${workspaceRoot}",
        "env": {
            "GOOS": "windows",
            "GOARCH": "amd64",
            "GOPATH": "${workspaceRoot}",
            "CGO_ENABLE": "1",
            "PKG_CONFIG_PATH": "C:/oracle/instantclient_12_1"
        }
    }
}

launch.json // for debugging

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "{your program}",
            "env": {
                "GOPATH": "${workspaceRoot}",
                "ORACLE_SID": "xe",
                "ORACLE_HOME": "C:/oracle/instantclient_12_1",
                "ORACLE_BASE": "C:/oracle/instantclient_12_1"
                "NLS_LANG": ""
            },
            "args": [],
            "showLog": true
        }
    ]
}
@ikaltashev
Copy link

Thank's, perfect.
If it does not work, try

$ pkg-config --cflags oci8
Package oci8 was not found in the pkg-config search path.
Perhaps you should add the directory containing `oci8.pc'
to the PKG_CONFIG_PATH environment variable
No package 'oci8' found
$ pkg-config --debug
Option --debug seen
Error printing disabled by default, value of --print-errors: 0
Error printing disabled
Adding virtual 'pkg-config' package to list of known packages
Cannot open directory 'C:/Program Files/Git/mingw64/lib/pkgconfig' in package search path: No such file or directory
Cannot open directory 'C:/Program Files/Git/mingw64/share/pkgconfig' in package search path: No such file or directory
Cannot open directory 'C:/TDM-GCC-64/lib/pkgconfig' in package search path: No such file or directory
Cannot open directory 'C:/TDM-GCC-64/share/pkgconfig' in package search path: No such file or directory
Must specify package names on the command line

copy oci8.pc to C:/TDM-GCC-64/lib/pkgconfig

$ pkg-config --list-all
oci8 oci8 - Oracle database engine
$ pkg-config --cflags oci8
-IC:/oracle/instantclient_12_2/sdk/include -IC:/TDM-GCC-64/include

@amiracam
Copy link

I get the following error:
PS C:\instantclient_11_2> dlltool -D oci.dll -d oci.def -l sdk/lib/gcc/liboci.a
ÿ_C:\TDM-GCC-64\bin\dlltool.exe: Syntax error in def file oci.def:0
PS C:\instantclient_11_2>

note that "dlltools" does not exist under TDM-GCC but rather "dlltool"

line 0 , simply has this:

LIBRARY OCI.dll

any ideas ? thanks

@bearx3f
Copy link
Author

bearx3f commented Sep 16, 2017

@amiracam something wrong with your oci.def could you show oci.def file's content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment