clausecker / nfc

Go bindings for the libnfc
https://pkg.go.dev/github.com/clausecker/nfc/v2
GNU Lesser General Public License v3.0
101 stars 20 forks source link

Help with cross compiling for Raspberry Pi #10

Closed vagmi closed 7 years ago

vagmi commented 7 years ago

I am trying to cross compile a simple go program.

package main

import (
    "fmt"
    "github.com/fuzxxl/nfc/2.0/nfc"
)

func main() {
    fmt.Println(nfc.Version())
    devices, err := nfc.ListDevices()
    if err != nil {
        fmt.Print(err)
        return
    }
    for _, d := range devices {
        fmt.Printf("found device %s", d)
    }
}

I am cross compiling it with the following command.

GOOS=linux GOARCH=arm go build -a -v github.com/fuzxxl/nfc/2.0/nfc

I am using the golang:1.7.5 docker image (based on debian wheezy) for compilation. I encounter the following compiler error.

../../fuzxxl/nfc/2.0/nfc/nfc.go:206: undefined: context

It is clear that etc.go has type context struct defined in the nfc package. This error occurs only during cross compilation. Is there something that I am missing?

clausecker commented 7 years ago

The libnfc wrapper uses cgo to access the libnfc. By default, cgo is disabled for cross-compilations, leading to the error you observe. If you want to cross compile the nfc wrapper, you need to provide a C cross-compiler and a compiled copy of the libnfc and set CC_FOR_TARGET as outlined in this document.

I recommend you to compile the libnfc and this wrapper on your Raspberry Pi instead of trying to cross-compile it.