go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work.

Open piotrkochan opened this issue 5 years ago • 16 comments

go-sqlite3 - tag v1.14.3

I'm building app in the goreleaser in docker environment. I read on README:

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.

I'm running:

CGO_ENABLED=1 go install github.com/mattn/go-sqlite3
goreleaser --snapshot --rm-dist

goreleaser.yml:

builds:
  binary: [...]
  env:
  - CGO_ENABLED=0
  goarch:
    - amd64
  goos:
  - linux

and the error is:

./myapp
2020-09-17T14:24:42.648+0200    INFO    app/db.go:15    using database ./app.sqlite
panic: Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

piotrkochan avatar Sep 17 '20 12:09 piotrkochan

CGO_ENABLED=1 go install github.com/mattn/go-sqlite3
go build
./app

gives same error so it is not related to the goreleaser itself

piotrkochan avatar Sep 17 '20 12:09 piotrkochan

I don't think the statement in the README is true. (Or at least, isn't true anymore.)

CGO_ENABLED=1 go install github.com/mattn/go-sqlite3
go build
./app

In this example, when you run go build (presumably with CGO_ENABLED=0), the Go compiler recompiles this library without cgo. To my knowledge, there is no way to force the Go compiler to use a pre-built version of a library, and if detects any changes (including enabling/disabling cgo), it will recompile.

rittneje avatar Sep 17 '20 14:09 rittneje

I'm facing the same issue.

stefins avatar Jan 21 '21 09:01 stefins

same issues here

ghost avatar Aug 25 '22 00:08 ghost

I fixed it with adding CGO_ENABLED=1 but getting this to work with windows or even some cpu arch is challenging! https://github.com/Clivern/Chaos/blob/main/.goreleaser.yml#L19-L27

Clivern avatar Feb 22 '23 14:02 Clivern

I ran into this using go run . Had to run apt install build-essential on the host and then all was well.

aviddiviner avatar Apr 04 '23 15:04 aviddiviner

@aviddiviner Thanks man my tests were not building I was using sqlite database for go application, getting this error -

failed to initialize database, got error %v, /var/lib/jenkins/workspace/GoBuildTest/repository/repository.go:80%!(EXTRA *errors.errorString=Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub)

I was using it as a part of jenkins build process, and my jenkins server was running on EC2 ubuntu machine, with build-essential this worked fine.

bindian0509 avatar Apr 04 '23 19:04 bindian0509

If you are using M1 Mac and have specified the environment variable GOARCH=amd64, you will need to change it as follows. (This is my minor past solution...)

go env -w GOARCH=arm64

Hirosaji avatar Apr 25 '23 07:04 Hirosaji

same issues here

matiasdev30 avatar Dec 09 '23 19:12 matiasdev30

Has anyone managed to solve this in a windows environment?

lucasskt18 avatar Jan 08 '24 20:01 lucasskt18

Has anyone managed to solve this in a windows environment?

go env -w CGO_ENABLED=1

ayoubzulfiqar avatar Feb 16 '24 09:02 ayoubzulfiqar