question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLite3 usage with CGO_ENABLE build flag error

See original GitHub issue

Summary

Attempting to use the SQLite3 database backend for sharing links fails because the binary was not complied with CGO_ENABLED=1.

Goal

The project advertises the ability to use SQLite3 as a backend for the link sharing functionality.

Expected Outcome

When the binary or docker container is ran with the following parameters, the sharable links will be stored in an SQLite3 database in the given location: --shared-links.sql.driver="sqlite3" --shared-links.sql.dsn="/PATH".

Actual Outcome

The program throws the following error and fails to run: caller=main.go:191 level=error msg="Error initializing link sharer." err="error creating SQL link sharer: error enabling foreign key support: Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub"

Reproduction

The below demonstrates a method of reproducing this issue, using both the official standalone binary and the Docker container.

Standalone Binary (downloaded from GitHub release)

~/promlens-0.1.0.linux-amd64$ sha256sum promlens
f2c9a5ae428aa0a3a7097b8422b946a0289cb09bf2cebe952af4cfb78f6463ae  promlens
~/promlens-0.1.0.linux-amd64$ file promlens
promlens: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=K5nNTk_EsJ11Qmo3OmoO/Z9WIbgyQRzHs5Bzv3Nbq/vrkUmd1oyhisKw0i8n6c/OsFH0xvAPR6Nx3LVqG4n, with debug_info, not stripped
~/promlens-0.1.0.linux-amd64$ ./promlens --shared-links.sql.driver="sqlite3" --shared-links.sql.dsn="~"
caller=main.go:191 level=error msg="Error initializing link sharer." err="error creating SQL link sharer: error enabling foreign key support: Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub"

Docker Container

$ docker run --rm prom/promlens --shared-links.sql.driver="sqlite3" --shared-links.sql.dsn="/tmp/promlens-links.db"
caller=main.go:191 level=error msg="Error initializing link sharer." err="error creating SQL link sharer: error enabling foreign key support: Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub"

Additional Information

The go-sqlite3 repo contains the following text confirming the above:

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. (https://github.com/mattn/go-sqlite3#installation)

Investigation

I have attempted to build the binary myself with the CGO_ENABLED flag set to 1 in several ways with no success.

These include setting system and Go environment variables:

# go env | grep CGO_ENABLED && env | grep CGO_ENABLED
CGO_ENABLED="1"
CGO_ENABLED=1

And updating line 198 of Makefile.common to the following: CGO_ENABLED=1 $(PROMU) build --prefix $(PREFIX) $(PROMU_BINARIES).

I have also confirmed that the build process (specifically $(PROMU) build --prefix $(PREFIX) $(PROMU_BINARIES)) had the CGO_ENABLED environment variable set to 1, by reviewing the process’ environment file, at /proc/#####/environ.

I have also confirmed that gcc is installed on the system that the above build was attempted on.

Despite all of these settings, a rebuilt binary still reports the same error.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
SuperQcommented, Oct 27, 2022

I got a PR half done to switch to the other sqlite library. I’ll finish it up and send it out.

0reactions
Kickballcommented, Oct 28, 2022

If you’re using make build, we take the CGO setting from the .promu.yaml.

Well that would explain it! Thanks for the info 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

gox - throws error for sqlite3 and all the builds are failed
I tried with GOOS=linux GOARCH=arm CGO_ENABLED=1 go build -o pravasan pravasan.go but got an error # runtime/cgo clang: error: argument unused during ...
Read more >
Cross compiling fails · Issue #384 · mattn/go-sqlite3 - GitHub
If I understood it right CGO_ENABLED=1 requires a C cross compiler? Is enabled cgo a new dependency if I want to use your...
Read more >
go-sqlite3 requires cgo to work : r/golang - Reddit
The test cases build and run fine until I add a dependency on a sqlite… ... error: Binary was compiled with 'CGO_ENABLED=0', go-sqlite3...
Read more >
go-sqlite3 - mattn - Google Git
A sqlite3 driver that conforms to the built-in database/sql interface. ... you are required to set the environment variable CGO_ENABLED=1 and have a...
Read more >
Catch error when using SQLite in Golang - DEV Community ‍ ‍
I was working on a project using Go and SQLite. For the driver part, ... I want to build a release for linux...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found