Closed kylewolfe closed 6 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.
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
@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?
@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.
@ricsmania man you are real rockstar i was going crazy with this oci8, thanks it worked
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=
@MichaelS11 Thanks!
Welcome :)
@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?
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.
@MichaelS11 can you share these guides? Thanks anyway, your solution is better than mine :)
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://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.
@MichaelS11 Nice, thanks!
Welcome :)
Closing as Linux -> Windows has been addressed, and I no longer need Windows -> Linux solution.
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.
Moving the resulting binary to Linux, I get the following result:
Is cross compilation possible or do I need to rebuild on the target architecture?