Building Static Binary with ODBC in Go - go

I'm kinda new to the concept of static building in go.
I was trying to build a static binary for Linux that includes ODBC (which needs cgo) inside of it and was failing to do that. This is the library I'm trying to build with alexbrainman/odbc.
I have tried a couple commands, not sure if they even fit the scenario here;
CGO_ENABLED=0 GOOS=linux go build -a -ldflags="-s -w"
CGO_ENABLED=0 go build -ldflags '-linkmode external -extldflags "-fno-PIC -static"'
CGO_ENABLED=0 go build -ldflags '-w -extldflags "-static"'
They all fail just like running CGO_ENABLED=0 go build.
Is this even possible to do?
Thanks

Related

Flags needed to create static binaries in golang

I was building a Docker image using scratch as base.
The following build command:
RUN go build -o /go/bin/myapp
created a binary that kept failing when executed:
standard_init_linux.go:211: exec user process caused "no such file or directory"
By trial and error I found out that I needed to build as follows:
RUN CGO_ENABLED=0 go build -o /go/bin/myapp -a -ldflags '-extldflags "-static"' .
Why are both CGO_ENABLED=0 and -ldflags '-extldflags "-static"' necessary?
Don't both options create static binaries? (i.e. binaries that will need absolutely nothing in terms of libraries from the environments they will run on?)
Just before we start, a heads up by Russ Cox's: Comment
Read this as well: Comment
Yes, I agree with Volker's comment that some systems don't really allow static binaries.
Read on: Compile packages and dependencies
-a
force rebuilding of packages that are already up-to-date.
-ldflags '[pattern=]arg list'
arguments to pass on each go tool link invocation.
Read: go tool link
-extldflags flags
Set space-separated flags to pass to the external linker.
Hence, it tries to rebuild all the packages (dependencies as well) with CGO disabled and also -static means do not link against shared libraries.
Some of the points related to static linking are explained well: Linking golang statically

How to build SO library for ARM architecture

I need to build a shared library in Go. For this purpose, I used CGO and then built SO lib with options
go build -o libUtil.so -buildmode=c-shared main.go
Now, I need to do the same, but for ARM architecture. When I do not use CGO, I only do export GOARCH=arm
and this is enough to succeed. However, when I use CGO, I can not build SO library.
I suspect, that I need to install arm build tools, but I don`t know how to do that and how to configure my GO environment to use these tools. I hope, somebody might help me.
OS is Linux.
It may be different in you own *.nix system, but in common case your settings shoul de like below:
export CC=arm-linux-gnueabi-gcc //choose your arm compiler binary
export GOARCH=arm
export CGO_ENABLED=1
then you cab build:
go build -o yourLib.so -buildmode=c-shared main.go

how to build hyperledger fabric with gccgo

Is there a way to build hyperledger fabric using gccgo? I want to do this in order to use -finstrument-functions option of gcc to trace function calls. But I encountered two problems. My steps are as follows.
find the build command
make -n release
echo "Building release/linux-amd64/bin/configtxgen for
linux-amd64"
mkdir -p release/linux-amd64/bin CGO_CFLAGS=" "
GOOS=linux GOARCH=amd64 go build -o
/home/yiifburj/go/src/github.com/hyperledger/fabric/release/linux-amd64/bin/configtxgen
-tags "nopkcs11" -ldflags "-X github.com/hyperledger/fabric/common/tools/configtxgen/metadata.Version=1.1.0"
github.com/hyperledger/fabric/common/tools/configtxgen
modify the build command to use gccgo
CGO_CFLAGS=" " GOOS=linux GOARCH=amd64 go build -compiler gccgo -o /home/yiifburj/go/src/github.com/hyperledger/fabric/release/linux-amd64/bin/configtxgen
-tags "nopkcs11" -gccgoflags "-X github.com/hyperledger/fabric/common/tools/configtxgen/metadata.Version=1.1.0"
github.com/hyperledger/fabric/common/tools/configtxgen
#github.com/hyperledger/fabric/bccsp/factory
bccsp/factory/pluginfactory.go:12:8: error: import file ‘plugin’ not
found
"plugin"
bccsp/factory/pluginfactory.go:56:15: error: reference to undefined name
‘plugin’ plug, err :=
plugin.Open(config.PluginOpts.Library)
First, as above, "plugin" couldn't be found when gccgo is invoked by go build.
Another one is how to pass ldflags -X when use gccgo? It seems that -X is a parameter only invalid in gc tools not gccgo.
Anyone can help me? Thanks.
You'll need to use the correct version of GCC in order to get support for the versions of Go supported by the various Fabric releases.
Fabric 1.1 requires Go 1.9.x
Fabric 1.2.x requires Go 1.10.x
The upcoming Fabric 1.3 also requires Go 1.10
gccgo did not support Go 1.9 (GCC 7 had support for Go 1.8). GCC 8 adds support for Go 1.10.
So you should use Fabric v1.2 and GCC 8.

golang program import "github.com/ethereum/go-ethereum/accounts/keystore" build error

Please find my golang specifications
GOOS=linux
GOARCH=amd64
go build github.com/ethereum/go-ethereum/crypto/secp256k1: build
constraints exclude allGo files in
/Users/mac/Documents/project/src/github.com/ethereum/go-ethereum/crypto/secp256k1
Run the command with CGO_ENABLED=1 to enable the use of cgo when cross compiling.
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build

Is there any way to compile and statically link ruby libs for Docker?

UPDATED 6 29 2015:
InfraRuby compiler and runtime for statically typed Ruby on the JVM!
I'd like to have a single binary, statically linked, out of a simple ruby crawler to build a slim docker container.
Something like what Go produces with:
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
(from Building Minimal Docker Containers for Go Applications).
Is there a way of doing this with ruby/rails?

Resources