d2r2/go-dht

cross compilations techniques compile error

volker-raschek opened this issue · 1 comments

I try to read out the data of my DHT11 sensor using the example. My Golang code looks like this:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/d2r2/go-dht"
)

func main() {
	temperature, humidity, err := dht.ReadDHTxx(dht.DHT11, 4, true)
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}
	// Print temperature and humidity
	fmt.Printf("Temperature = %v*C, Humidity = %v%%\n", temperature, humidity)
}

I downloaded the lib via go get -u github.com/d2r2/go-dht.

Thereby compile the binary on my Arch Linux system. Therefore I give the ARM, OS and ARCH variables so that the binary is executable on the Pi. When compelling I get the following error:

markus@markus-pc:~/go/src/dht11-example$ GOARM=7 GOOS=linux GOARCH=arm go build
./main.go:17:32: undefined: dht.ReadDHTxx
./main.go:17:46: undefined: dht.DHT11

Does anyone have any idea what I'm doing wrong?

d2r2 commented

Hi @volker-raschek! Sorry, for delayed response.

You should remember, that since DHT library partially written in C (and your native system has no ARM7 architecture), and you employ "cross compilation techniques", so you should have on your system special cross c-compiler installed. The name of c-compiler for Debian/Ubuntu system will be arm-linux-gnueabi-gcc, for Arch Linux arm-linux-gnueabihf-gcc.

So, once you have this c-compiler, you must modify your compilation call accordingly:

  • for Debian/Ubuntu: CC=arm-linux-gnueabi-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7|6 go build
  • for Arch Linux: CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7|6 go build

Note: you should select ARM version 6 or 7. Regular Raspberry PI ordinary has 6.

I have one on my Arch Linux notebook where i'm doing all the development (and cross compilation works good), and as I remember arch ecosystem has this compiler only in AUR repository, so you should install it from here:
https://aur.archlinux.org/packages/arm-linux-gnueabihf-gcc/

I think the hardest part here is installing a с-compiler, so try and let me know if you still need help.