mattn / go-oci8

Oracle driver for Go using database/sql
https://mattn.kaoriya.net/
MIT License
631 stars 212 forks source link

Cross compiling go-oci8? #84

Closed kylewolfe closed 6 years ago

kylewolfe commented 8 years ago

I'm sure this is probably not related directly to go-oci8, but I'll ask here. I have a simple example project building correctly on Windows x64 (Go 1.6). I'm now looking to cross compile for Linux x64.

$ env GOOS=linux go build -v
runtime
errors
math
sync/atomic
unicode/utf8
sort
unicode
sync
io
syscall
bytes
strings
time
os
strconv
reflect
fmt
database/sql/driver
database/sql
github.com/mattn/go-oci8
local/go-ora

Moving the resulting binary to Linux, I get the following result:

sql: unknown driver "oci8" (forgotten import?)

Is cross compilation possible or do I need to rebuild on the target architecture?

mattn commented 8 years ago

do you have cross-compiling environment? go-oci8 uses cgo so you must have C compiler for windows. Also pkg-config for windows.

ricsmania commented 8 years ago

I never cross compiled from Windows to Linux, but I did compile from Linux to Windows using Docker and it works perfectly.

Did you set the CGO_ENABLED environment variable?

Have you thought about using Docker? It might be a better option. If you are interested this is the Dockerfile I use to compile Linux binaries using go-oci8:

FROM golang

ADD /instantclient_12_1 /usr/instantclient_12_1

RUN apt-get update
RUN apt-get install -y pkg-config
RUN apt-get install -y libaio1

ADD oci8.pc /usr/lib/pkgconfig/oci8.pc

ENV LD_LIBRARY_PATH /usr/lib:/usr/local/lib:/usr/instantclient_12_1

RUN ln -s /usr/instantclient_12_1/libclntsh.so.12.1 /usr/instantclient_12_1/libclntsh.so
RUN ln -s /usr/instantclient_12_1/libclntshcore.so.12.1 /usr/instantclient_12_1/libclntshcore.so
RUN ln -s /usr/instantclient_12_1/libocci.so.12.1 /usr/instantclient_12_1/libocci.so

RUN go get -u github.com/mattn/go-oci8

And oci8.pc:

prefix=/usr
includedir=${prefix}/instantclient_12_1/sdk/include
libdir=${prefix}/instantclient_12_1

Name: oci8
Description: Oracle Instant Client
Version: 12.1
Cflags: -I${includedir}
Libs: -L${libdir} -lclntsh

In order to build the image you need to download Oracle Instant Client and put it in the instantclient_12_1 folder

kylewolfe commented 8 years ago

@mattn, I do have those prereqs fulfilled, and I've built successfully for Windows already.

Thank you @ricsmania. I did not have CGO_ENABLED set. After setting I get the following:

$ env GOOS=linux go build -v
runtime
errors
math
sync/atomic
unicode/utf8
sort
unicode
sync
runtime/cgo
io
syscall
# runtime/cgo
C:\Users\wolfek9\AppData\Local\Temp\go-build731452338\runtime\cgo\_obj\_cgo_main.c:1:0: error: -fPIC ignored for target (all code is position independent) [-Werror]
 int main() { return 0; }
 ^
cc1.exe: all warnings being treated as errors
bytes
strings
time
strconv
os
reflect
fmt
database/sql/driver
database/sql

Any idea what might be causing this?

rof20004 commented 7 years ago

@ricsmania How you compile from Linux to Windows, I get the same error today, from Linux to Windows:

GOOS=windows GOARCH=amd64 go build

The error after move the executable to Windows and double click on it:

sql: unknown driver "oci8" (forgotten import?)

Obs.: I am not using docker and I do not want to use.

vk1818 commented 7 years ago

@ricsmania man you are real rockstar i was going crazy with this oci8, thanks it worked

MichaelS11 commented 6 years ago

Cross compile on Linux to Windows

After way too many hours, here is what worked for me to compile Linux and Windows on a Linux box.

# install needed yum packages, here are the main ones:
yum install git gcc gcc-go rpm-build mingw64-gcc mingw64-gcc-c++

# install Linux instant client 11g 64 bit from rpm to normal location
# install Windows instant client 11g 64 bit to /usr/local/oracle/instantclient_11_2_64

# May need to fix Windows instant client int64 types
sed -i 's/typedef unsigned _int64 ubig_ora;/typedef unsigned __int64 ubig_ora;/g' /usr/local/oracle/instantclient_11_2_64/sdk/include/oratypes.h
sed -i 's/typedef   signed _int64 sbig_ora;/typedef   signed __int64 sbig_ora;/g' /usr/local/oracle/instantclient_11_2_64/sdk/include/oratypes.h

# setup package config for Linux version
export PKG_CONFIG_PATH="/go/pkg_config"
mkdir $PKG_CONFIG_PATH
echo -e "\nName: oci8\nDescription: oci8\nLibs: -L/usr/lib/oracle/11.2/client64/lib/ -lclntsh\nCflags: -I/usr/include/oracle/11.2/client64/\nVersion: 11.2\n" > $PKG_CONFIG_PATH/oci8.pc

# build Linux 64 bit
go build -v -o myprogram mypackage

# build Windows 64 bit
export CGO_ENABLED=1
export CC=x86_64-w64-mingw32-gcc
export CXX=x86_64-w64-mingw32-g++
export CGO_CFLAGS="-I/usr/local/oracle/instantclient_11_2_64/sdk/include/"
export CGO_LDFLAGS="-L/usr/local/oracle/instantclient_11_2_64/ -L/usr/local/oracle/instantclient_11_2_64/sdk/lib/msvc/ -lstdc++ -loci"
GOOS=windows GOARCH=amd64 go build -v -tags noPkgConfig -o myprogram.exe mypackage
export CGO_ENABLED=
export CC=
export CXX=
export CGO_CFLAGS=
export CGO_LDFLAGS=
rof20004 commented 6 years ago

@MichaelS11 Thanks!

MichaelS11 commented 6 years ago

Welcome :)

rof20004 commented 6 years ago

@MichaelS11 A question:

With this, I do not need to install instant client to the systems? I mean, the binary generated have all driver needs?

MichaelS11 commented 6 years ago

When building the program, need either the instant client, full client, or Oracle database on the system.

When running the built program, only need a few items from the Oracle software, but any of the above three should work as well.

For Windows it seem to only need: oci.dll and oraociei11.dll. For Linux seem to need: glogin.sql, libclntsh.so.11.1, libheteroxa11.so, libnnz11.so, libocci.so.11.1, libociei.so, libocijdbc11.so, libsqlplus.so, libsqlplusic.so, libsqora.so.11.1

Note that that you can build it static so all the required file are inside the binary, but to do so is much more complicated. I have not spent the time to figure it out but have glanced at a few guides on how other people have done it.

rof20004 commented 6 years ago

@MichaelS11 can you share these guides? Thanks anyway, your solution is better than mine :)

MichaelS11 commented 6 years ago

When I was looking into it I had just done an internet search. I did not save the results. Here are a couple of links I found just by doing another internet search.

https://stackoverflow.com/questions/38785691/trying-to-build-static-cgo-executable-with-oracle-libraries-on-linux-ubuntu

https://github.com/mattn/go-oci8/issues/102

I am sure there is more information out there that you can find, just would need to spend time looking for it.

rof20004 commented 6 years ago

@MichaelS11 Nice, thanks!

MichaelS11 commented 6 years ago

Welcome :)

kylewolfe commented 6 years ago

Closing as Linux -> Windows has been addressed, and I no longer need Windows -> Linux solution.