mattn / go-sqlite3

sqlite3 driver for go using database/sql
http://mattn.github.io/go-sqlite3
MIT License
7.91k stars 1.1k forks source link

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

Open piotrkochan opened 4 years ago

piotrkochan commented 4 years ago

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 commented 4 years ago
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

rittneje commented 4 years ago

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.

stefins commented 3 years ago

I'm facing the same issue.

ghost commented 2 years ago

same issues here

Clivern commented 1 year ago

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

aviddiviner commented 1 year ago

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

bindian0509 commented 1 year ago

@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.

Hirosaji commented 1 year ago

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

matiasdev30 commented 9 months ago

same issues here

lucasskt18 commented 8 months ago

Has anyone managed to solve this in a windows environment?

ayoubzulfiqar commented 7 months ago

Has anyone managed to solve this in a windows environment?

go env -w CGO_ENABLED=1

margani commented 2 months ago

I had this issue in Windows and did the following to fix it:

tl;dr:

  1. Run go env -w CGO_ENABLED=1
  2. Download and install tdm64-gcc-5.1.0-2.exe with default options (to add to PATH env variable), and restart your terminal or IDE session.

Longer explanation:

maximka200 commented 1 month ago

I has this issue in Windows and did the following to fix it:

tl;dr:

  1. Run go env -w CGO_ENABLED=1
  2. Download and install tdm64-gcc-5.1.0-2.exe with default options (to add to PATH env variable), and restart your terminal or IDE session.

Longer explanation:

  • Thanks to @ayoubzulfiqar, I ran go env -w CGO_ENABLED=1
  • Then when I run go run ., it shows the error: cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH%
  • I search the web, and found an article that I can use MinGW (mingw-get-setup.exe), and I followed the article, and I had GCC available, but then go run . didn't work, it showed the error: cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
  • So I searched the error, and found this SO answer that said TDM-GCC only works in Windows x64, so I downloaded tdm64-gcc-5.1.0-2.exe and installed it, and removed MinGW from my System PATH environment variable, and now I can build and run my go app using go-sqlite3 package.

it works, thx

Kaloszer commented 1 month ago

@margani works! Thanks

Note that the installer tells you to get the new ver from (which also works): https://jmeubank.github.io/tdm-gcc/download/