gomipscross-compilinglibpcapgopacket

How to compile a Go application using gopacket for 32bit mips


I am trying to compile a little application using the gopacket library to linux on a 32bit mips cpu. Unfortunately I am getting loads of errors like this:

/home/cdutz/go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap.go:30:22: undefined: pcapErrorNotActivated

On a "normal" linux system these seem to be defined in "pcap_unix.go" while on windows the values are coming from "defs_windows_amd64.go". I do have the libpcap in a 32bit mips version on my target system, which is good as my target system doesn't have the extra space to install all the tools needed to compile anything on it. I know that libpcap doesn't exist as a 1-to-1 version on windows, so this probably explains the "defs"-files. But I would generally expect it to have the same API as the one on my linux system.

[UPDATE] So it seems number 1 of the things that needs to be ensured is that cgo is executed. This is done by setting the environment variable:

CGO_ENABLED=1

Next we need to ensure the mips compatible versions of the libpcap are installed (the header files are identical on any architecture). In order to do this on my Ubuntu 16.4 I first needed to enable the 'mips' archirecture:

dpkg --add-architecture mips

And add the debian repo to the /etc/apt/sources.list

deb [trusted=yes] http://ftp.de.debian.org/debian buster main

As soon as that's done, I could install the mips binaries:

apt install libpcap-dev:mips libpcap0.8-dev:mips libc6-dev:mips libdbus-1-dev:mips libpcap0.8:mips

In order to cross compile, it seems I need a gccgo version that can do that. For this I installed:

apt-get install gccgo-mips-linux-gnu

Now comes something I am not sure I did right, but when running the go build with compiler=gccgo it always picked the amd64 version and using anything else but 'gccgo' as compiler argument didn't work, so I updated the symlink in /usr/bin/gccgo to point to: 'mips-linux-gnu-gccgo-8' (in the same directory).

After all of these changes, I was able to almost build everything with this command:

go build -compiler=gccgo

If I enable the additional output with the '-x' option, I can see that cgo is now doing it's thing. It's also compiling all the other modules. But on the pcap one it now fails with:

cc1: error: command line option '-c' is valid for the driver but not for C

So this is where I'm currently stuck at.


Solution

  • Ok, after 3 days I think I managed to get things working and I'll summ up what I did.

    In the end the gccgo path was a dead end, so instead of installing gccgo-mips-linux-gnu I installed gcc-mips-linux-gnu.

    Next I set the CC environment variable to point to this:

     export CC=/usr/bin/mips-linux-gnu-gcc-8
    

    That was actually what was missing.

    So summing it up on my Ubuntu 16.04 system:

    dpkg --add-architecture mips
    echo "deb [trusted=yes] http://ftp.de.debian.org/debian buster main" > /etc/apt/sources.list
    apt update
    apt install -y wget git build-essential mc
    apt install -y gcc-mips-linux-gnu
    apt install -y libpcap-dev:mips libpcap0.8-dev:mips libc6-dev:mips libdbus-1-dev:mips libpcap0.8:mips
    
    export CC=/usr/bin/mips-linux-gnu-gcc-8
    export GOOS=linux
    export GOARCH=mips   
    export GOMIPS=softfloat
    export CGO_ENABLED=1
    
    go build
    

    I hope this might help others.