go-sqlite3: Cross compiling fails

Very possibly a mistake on my part, if so please let me know where I went wrong. My first time with Go today!

I have a basic Go file using this library. I am on macOS 10.12.3, Go 1.8 darwin/amd64.

Succeeds: env GOOS=darwin go build -i app.go

Fails: env GOOS=linux go build -i app.go

Specifying GOARCH=amd64 makes no difference.

Errors:

../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:18: undefined: SQLiteConn
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:26: undefined: SQLiteConn
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:27: undefined: namedValue
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:29: undefined: namedValue
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:35: undefined: SQLiteConn
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:36: undefined: namedValue
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:44: undefined: SQLiteConn
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:49: undefined: SQLiteConn
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:54: undefined: SQLiteStmt
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:63: undefined: SQLiteStmt
../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:36: too many errors

Said very basic Go file causing this, in-case it helps:

package main

import (
	"database/sql"
	"log"
	"os"
	_ "github.com/mattn/go-sqlite3"
)

// Bookmark represents a single row from database
type Bookmark struct {
	Id       int
	Url      string
	Metadata string
	Tags     string
	Desc     string
	Flags    int
}

func main() {
	dbpath := GetDbPath()

	db := InitDB(dbpath)
	defer db.Close()

	bookmarks := GetAllBookmarks(db)
	log.Print(bookmarks)
}

// Write errors to log & quit
func CheckError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func InitDB(filepath string) *sql.DB {
	db, err := sql.Open("sqlite3", filepath)
	CheckError(err)
	if db == nil {
		panic("db nil")
	}
	return db
}

func GetAllBookmarks(db *sql.DB) []Bookmark {
	const queryAll string = "SELECT * FROM bookmarks;"

	rows, err := db.Query(queryAll)
	CheckError(err)
	defer rows.Close()

	var result []Bookmark
	for rows.Next() {
		item := Bookmark{}
		err2 := rows.Scan(&item.Id, &item.Url, &item.Metadata, &item.Tags, &item.Desc, &item.Flags)
		CheckError(err2)
		result = append(result, item)
	}

	return result
}

func GetDbPath() string {
	const dbFileName string = "bookmarks.db"

	var dir = os.Getenv("XDG_DATA_HOME")
	if dir != "" {
		dir += "/buku/"
	} else {
		dir = os.Getenv("HOME") + "/.local/share/buku/"
	}

	dir += dbFileName

	return dir
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 40 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I experienced the same problems as described by this thread, but I was eventually able to get things to work.

From a linux amd64 host, I was finally able to cross compile for linux arm. Posting below, in case it may be useful for anyone.

## Building ARM Binary
export GOOS=linux; \
export GOARCH=arm; \
export GOARM=7; \
export CC=arm-linux-gnueabihf-gcc-5; \
CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static" \
  -o "./bin/${GOOS}_${GOARCH}/the_app"

I succeeded to cross compile from macos to linux using musl-cross. brew install FiloSottile/musl-cross/musl-cross

using this command line:

CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ GOARCH=amd64 GOOS=linux CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static"

Hope this helps others.

What is bad? sqlite3 is written in C code. so You MUST use CGO.

Maybe your CGO_ENABLED is 0

I succeeded to cross compile from macos to linux using musl-cross. brew install FiloSottile/musl-cross/musl-cross

using this command line:

CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ GOARCH=amd64 GOOS=linux CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static"

Hope this helps others.

THANKS! Really saved my day~ For those who want to compile ARM like Raspberry pi: brew install FiloSottile/musl-cross/musl-cross --without-x86_64 --with-arm-hf You can also use --with-i486 (x86 32-bit), --with-aarch64 (ARM 64-bit), --with-arm (ARM soft-float) and --with-mips. But it takes a lot of time: on my old Mac built in 174 minutes 44 seconds BTW, my build command is CC=arm-linux-musleabihf-gcc CXX=arm-linux-musleabihf-g++ GOARCH=arm GOARM=7 GOOS=linux CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static" xxx.go

@gabrielruiu install gcc-5-arm-linux-gnueabihf. That package is not available on debian 9 (stretch) so I used gcc-6-arm-linux-gnueabihf, which worked fine for me, I just had to change CC=arm-linux-gnueabihf-gcc-6.

With go 1.7 I can cross compile without a C cross compiler (like mingw). I can use the native go compiler and have no problems.

# This works, but only with go 1.7
# Not with go 1.8
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

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 package with go 1.8?

Thanks for your work and your time!

Should it be possible to cross compile go-sqlite without enabled CGO?

go-sqlite3 is CGO module. So you need to enable CGO always.

Hi Mattn,

I have the same problem with go 1.8. If I downgrade to 1.7 there are no more problems. So I do not think there is a problem with the compiler.

OS: Linux x86_64

@carosatig 's comment was quite helpful in resolving compilation issues on OSX. I’d recommend documenting this.

@mattn Please let me know if you would be okay with me creating a PR that appends this information to README.md.

Can’t find the correct compiler arguments for OSX to linux using the clang compiler, help greatly appreciated thx

% env GOOS=linux CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static"
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)
% env GOOS=linux CGO_ENABLED=1 go build
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)