undefined: SQLiteConn when trying to build go app for armv7 - go

I have to compile a Go service for Ubuntu ARM-v7
When I compile it with
GOARCH=arm GOARM=7 go build -v -o release/edge_to_bc -ldflags '-s -w -extldflags "-static"' ./...
I get:
gitlab.com/company/edge_to_bc/vendor/github.com/hyperledger/fabric/bccsp/pkcs11
# gitlab.com/company/edge_to_bc/vendor/github.com/hyperledger/fabric/bccsp/pkcs11
vendor/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go:82:12: undefined: pkcs11.Ctx
vendor/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go:83:16: undefined: pkcs11.SessionHandle
gitlab.com/company/edge_to_bc/vendor/github.com/mattn/go-sqlite3
# gitlab.com/company/edge_to_bc/vendor/github.com/mattn/go-sqlite3
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:18:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:26:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:27:17: undefined: namedValue
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:29:13: undefined: namedValue
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:35:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:44:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:49:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:54:10: undefined: SQLiteStmt
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:63:10: undefined: SQLiteStmt
vendor/github.com/mattn/go-sqlite3/tracecallback_noimpl.go:8:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:29:13: too many errors
How should I fix it ?

Go-sqlite3 is cgo package.
If you want to build your app using go-sqlite3, you need gcc.
However, after you have built and installed go-sqlite3 with go install github.com/mattn/go-sqlite3 (which requires gcc), you can build your app without relying on gcc in future.
Important: because this is a CGO enabled package you are required to set the environment variable CGO_ENABLED=1 and have a gcc compile present within your path.
Source
Cross compiling from Ubuntu to ARM7:
sudo apt install \
libc6-armel-cross \
libc6-dev-armel-cross \
binutils-arm-linux-gnueabi \
libncurses5-dev \
gcc-arm-linux-gnueabihf
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \
go build -v
Using a docker image:
# Install docker
$ curl -fsSL https://get.docker.com -o get-docker.sh | sudo sh -
$ sudo usermod -aG docker your-user
# Go to your project folder
$ cd your-project-folder
# Compile
$ docker run --rm \
-v /tmp/.docker/go:/go \
-v /tmp/.docker/go-build:/root/.cache/go-build \
-v $PWD:$PWD \
-w $PWD \
filipeandre/go-compiler-to-arm7:1.12 \
go build -v
Dockerfile

Related

libvirt-go cross-compiling fails on macos with the GOOS=Linux

I have a project using libvirt-go v7.4.0.
go.mod:
github.com/libvirt/libvirt-go v7.4.0+incompatible // indirect
It compiles on both mac and linux, but cross-compiling fails on macos with the GOOS=Linux flag.
$ env GOOS=linux go build -o myapp .
# gitlab.mycompany.io/mycompany-platform/myapp/command/agent
command/agent/libvirt_listener.go:11:26: undefined: libvirt.DomainEventType
command/agent/libvirt_listener.go:23:14: undefined: libvirt.Connect
command/agent/libvirt_listener.go:100:9: undefined: libvirt.EventRegisterDefaultImpl
command/agent/libvirt_listener.go:105:15: undefined: libvirt.NewConnect
command/agent/libvirt_listener.go:115:4: undefined: libvirt.EventRunDefaultImpl
command/agent/libvirt_listener.go:121:22: undefined: libvirt.Connect
command/agent/libvirt_listener.go:121:42: undefined: libvirt.Domain
command/agent/libvirt_listener.go:121:65: undefined: libvirt.DomainEventLifecycle
command/agent/libvirt_listener.go:149:63: undefined: libvirt.DomainState
command/agent/libvirt_listener.go:121:22: too many errors
THe libvirt-go package is a CGo binding to the native libvirt platform library. As such it is not possible to disable CGO when building it, and in turn it is not possible to cross-compile as that implicitly disables CGO.

Undefined when building for mips

Trying to cross compile my go app for mips.
GOOS=linux GOARCH=mipsle go build ./
However, I get the following issues:
# github.com/google/gopacket/afpacket
/root/go/src/github.com/google/gopacket/afpacket/options.go:176:19: undefined: pageSize
/root/go/src/github.com/google/gopacket/afpacket/options.go:177:85: undefined: pageSize
# github.com/google/gopacket/pcap
/root/go/src/github.com/google/gopacket/pcap/pcap.go:30:22: undefined: pcapErrorNotActivated
/root/go/src/github.com/google/gopacket/pcap/pcap.go:52:17: undefined: pcapTPtr
/root/go/src/github.com/google/gopacket/pcap/pcap.go:64:10: undefined: pcapPkthdr
What's the actual issue here? Is there some architecture limitations in gopacket that prevents me from building it for mips?
The problem is that gopacket is not a pure go project, it relies on CGO for running.
Enable CGO by passing env may be help:
CGO_ENABLED=1 GOOS=linux GOARCH=mipsle go build ./
At the same time, you need to prepare a mipsle version libpcap.
download cross complie toolchain: mipsle-linux-gcc
compile libpcap:
wget http://www.tcpdump.org/release/libpcap-1.8.1.tar.gz
tar -zxvf libpcap-1.8.1.tar.gz
cd libpcap-1.8.1
./configure --host=mipsle-linux --with-pcap=linux
make #if error occured, install dependency following the error message.
After compile, you'll find libpcap.a and libpcap.so.1.8.1 build up.
compile your project with libpcap linked
CGO_ENABLED=1 CC=mipsle-linux-gcc GOARCH=mipsle GOOS=linux CGO_LDFLAGS="-L./libpcap-1.8.1" go build ./

standard_init_linux.go:207: exec user process caused "no such file or directory" while trying to statically link c libs

I am unable to dockerize and use a utility written in c in go.
I have run this program locally without docker and it works
I tried using gccgo like so go build -compiler gccgo -gccgoflags -static-libgo but I get the same error
The preamble that calls the C functions looks like so:
/*
#cgo amd64 x86 LDFLAGS: -L. -lsomelib -lsomeotherlib
#include <stdio.h>
#include <stdlib.h>
#include "someheader.h"
*/
My docker file looks like so:
FROM golang:1.12 AS build
WORKDIR /go/src/app
COPY . .
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_LDFLAGS_ALLOW='-linkmode external -extldflags -static-libgcc'
COPY packageFolder $GOPATH/src/packageFolder
COPY mainPackage $GOPATH/src/mainPackage
RUN cd packageFolder
RUN go get -d -v ./...
RUN CGO_ENABLED=1 go build --ldflags '-linkmode external -extldflags -static-libgcc' -o $GOPATH/pkg/linux_amd64/packageFolder.a -x
RUN cd ../packageFolder
RUN go get -d -v ./...
RUN CGO_ENABLED=1 go build --ldflags '-linkmode external -extldflags -static-libgcc' -o $GOPATH/pkg/linux_amd64/mainPackage.a -x
RUN cd ..
RUN go get -d -v ./...
RUN go build -a -x
FROM ourPackager:latest AS packager
WORKDIR /
COPY ./resources ./resources/
RUN appman-packager create-package "package.tar.gz" ./resources
FROM scratch AS runtime
COPY --from=build /go/src/app/app /
COPY --from=packager "/package.tar.gz" ./resources/
EXPOSE 8080/tcp
ENTRYPOINT ["/app"]
I keep running into standard_init_linux.go:207: exec user process caused "no such file or directory" when I do a docker run
What am I missing?
I was able to fix it with Mark's suggestion. Using a Golang image for runtime exposed the actual problem of the shared object file not being packaged. So I copy it to /usr/lib/x86_64-linux-gnu in my runtime. I ended up using ubuntu:18.04 instead of the Golang image at runtime
FROM golang:1.12 AS build
WORKDIR /go/src/app
COPY . .
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=1
COPY acrcloud $GOPATH/src/packageFolder
COPY musicrec $GOPATH/src/mainPackage
RUN cd packageFolder
RUN go get -d -v ./...
RUN go build -o $GOPATH/pkg/linux_amd64/packageFolder -x
RUN cd ../mainPackage
RUN go get -d -v ./...
RUN go build -o $GOPATH/pkg/linux_amd64/mainPackage -x
RUN cd ..
RUN go get -d -v ./...
RUN go build -a -x
FROM ourPackager:latest AS packager
WORKDIR /
COPY ./resources ./resources/
RUN appman-packager create-package "package.tar.gz" ./resources
FROM ubuntu:18.04 AS runtime
COPY --from=build /go/src/app/app /
COPY --from=build /go/src/app/myExternalTool.so /usr/lib/x86_64-linux-gnu
COPY --from=packager "/package.tar.gz" ./resources/
EXPOSE 8080/tcp
ENTRYPOINT ["/app"]

cannot find package "github.com/gorilla/mux" in any of:

I use command go get github.com/gorilla/mux. I made http server using Golang, and I run this program :
package main
import (
"fmt"
"html"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
log.Fatal(http.ListenAndServe(":8080", router))
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
But I conflict this error :
/usr/local/go/bin/go build -i [/Users/imac/go/src]
http.go:9:5: cannot find package "github.com/gorilla/mux" in any of:
/usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
($GOPATH not set)
Error: process exited with code 1.
My Go environment is here :
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/imac/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v9/fkc_t97s5v1g9sr938zzvxvh0000gn/T/go-build096571864=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
I fight with this error for a week, But I can't find out solution. Please help me.
Could you try this steps to debug it:
ls -l /usr/local/go/src/github.com | grep gorilla
cd $GOPATH
go list ... | grep gorilla
if you din't see gorilla in the above two command, then you need to install it:
go get -v -u github.com/gorilla/mux
Please run this: export PATH=$PATH:$GOPATH/bin
How about running go run main.go ? is that working, if yes you should be able to do go build from your project path.
I wish this helpful.
You may off the 'mod'.
$ export GO111MODULE=off
This works for me...
fire command
> go mod init <your-directoryNane-where-main.go-exits>
fire command > go get github.com/gorilla/mux
after firing this both commands you will be able see 2 files :
go.mod
go.sum
finally, Close VS code and open again, the error will be resolved
Just remove quotes like this:
go get github.com/gorilla/mux
If you are using VS Code as your IDE and facing this problem:
VS Code uses $HOME/go as your default GOPATH - if you export another GOPATH you running into this trouble.
How to solve:
Use the VS Code internal terminal and navigate to your project folder: cd prjectFolder. Type go env and check if the GOPATH entry is the same as you get when you use cmd+t and then >Go: Current GOPATH
If it doesn't fit, add in your user settings:
"go.gopath": "/some/path"
where /some/path is the same path you export in you shell, zsh and so on.
Hope this helps.
I try to remove github.com/gorilla and github.com/peterbourgon directory, then, retry: make, it works.
<pre>
fail log:
mac#user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/client/assets/assets_debug.go \
assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/server/assets/assets_debug.go \
assets/server/...
go get -tags 'debug' -d -v ngrok/...
src/ngrok/server/config.go:16:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/gorilla/mux
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1
success log:
mac#user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/client/assets/assets_debug.go \
assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/server/assets/assets_debug.go \
assets/server/...
go get -tags 'debug' -d -v ngrok/...
github.com/gorilla/websocket (download)
github.com/gorilla/mux (download)
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1
mac#user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/client/assets/assets_debug.go \
assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/server/assets/assets_debug.go \
assets/server/...
go get -tags 'debug' -d -v ngrok/...
github.com/peterbourgon/diskv (download)
github.com/google/btree (download)
go install -tags 'debug' ngrok/main/ngrok
go install -ldflags "-s" -tags 'debug' ngrok/main/ngrokd
</pre>
Try go build /Users/imac/go/src/project
because I see you try to use go build under /Users/imac/go/src
Maybe this can help other users running in Windows.
In my case I had to create two symlinks:
1. run cmd as administrator
2. cd %gopath%
3. mklink /D src pkg\mod
this creates a symlink between src and pkg\mod
4. cd src\github.com\gorilla
Here you'll notice that the mux package might be listed as mux#v1.8.0
5. mklink /D mux mux#v1.8.0
With this, go will be able to find github.com/gorilla/mux
under %gopath%\src\github.com\gorilla\mux
Finally, you have to set GO111Module to off
set GO111Module=off
and now you can build your app:
go build app.go

Go tool unable to find binary. go tool: no such tool "vet"

I'm running golang in a docker container. And 'go tool' is unable to find 'vet'. Could you give me ideas on how to debug this?
I've used the Dockerfile for 1.5 as a template. https://github.com/docker-library/golang/blob/51d6eacd41fe80d41105142b9ad32f575082970f/1.5/Dockerfile
ENV GOLANG_VERSION 1.5.1
ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux- amd64.tar.gz
ENV GOLANG_DOWNLOAD_SHA1 46eecd290d8803887dec718c691cc243f2175fe0
RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
&& echo "$GOLANG_DOWNLOAD_SHA1 golang.tar.gz" | sha1sum -c - \
&& tar -C /usr/local -xzf golang.tar.gz \
&& rm golang.tar.gz
ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
However, when I install govet with
go get golang.org/x/tools/cmd/vet
and try
bash-4.3# go tool vet
go tool: no such tool "vet"
I have the following go environment set up:
$PATH includes $GOPATH/bin /usr/lib/go/bin:/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
bash# go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
bash# ls $GOPATH/bin
fgt go-junit-report godep golint mt-content-blogs vet
bash# ls $GOROOT/bin/
go gofmt
The crux of the issue is that go tools does not list vet, even after installing it with go get golang.org/x/tools/cmd/vet
bash# go tool
addr2line
api
asm
cgo
compile
dist
doc
fix
link
nm
objdump
pack
pprof
trace
yacc
Warning: starting Go 1.12 (February 2019, 3.5 years later), go tool vet won't be available at all. Only go vet.
See go 1.12 release notes:
The go vet command has been rewritten to serve as the base for a range of different source code analysis tools. See the golang.org/x/tools/go/analysis package for details.
A side-effect is that go tool vet is no longer supported.
External tools that use go tool vet must be changed to use go vet.
Using go vet instead of go tool vet should work with all supported versions of Go.
As part of this change, the experimental -shadow option is no longer available with go vet.
Checking for variable shadowing may now be done using:
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
go vet -vettool=$(which shadow)
Figured out the issue. It appears that I had missed installing go tools on the base docker image that I was using.
RUN apk --update-cache --allow-untrusted \
--repository http://dl-3.alpinelinux.org/alpine/edge/community/ \
--arch=x86_64 add \
go=${GOLANG_VERSION}-r3 \
go-tools=${GOLANG_VERSION}-r3 \
git \
&& rm -rf /var/cache/apk/* \
&& mkdir -p /go/src /go/bin \
&& chmod -R 777 /go

Resources