Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
piotrkochan opened this issue Sep 17, 2020 · 16 comments

Comments

@piotrkochan
Copy link

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
Copy link
Author

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
Copy link
Collaborator

rittneje commented Sep 17, 2020

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
Copy link

stefins commented Jan 21, 2021

I'm facing the same issue.

@ghost
Copy link

ghost commented Aug 25, 2022

same issues here

@Clivern
Copy link

Clivern commented Feb 22, 2023

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
Copy link

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

@bindian0509
Copy link

@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
Copy link

Hirosaji commented Apr 25, 2023

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
Copy link

same issues here

@lucasskt18
Copy link

Has anyone managed to solve this in a windows environment?

@ayoubzulfiqar
Copy link

Has anyone managed to solve this in a windows environment?

go env -w CGO_ENABLED=1

@margani
Copy link

margani commented Aug 4, 2024

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:

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

@maximka200
Copy link

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
Copy link

Kaloszer commented Aug 18, 2024

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

@nevenag
Copy link

nevenag commented Oct 7, 2024

Is there a workaround for docker on Mac M1 with amd64?

@prof79
Copy link

prof79 commented Oct 12, 2024

Heroes of the day!!! 🎉 @margani @Kaloszer @ayoubzulfiqar @jmeubank
Rarely seen guidance so to-the-point and excellent installer of the tdm-gcc team, instantly up and running
Go really has a slick (and sick ^^) ORM with GORM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests