7

I am trying to use gopacket on my windows 10.
I'm using it to sniff and inject packets directly to/from the NIC.
I can easily compile and run my code with GOARCH=386 but can't in GOARCH=amd64.

Worth noticing: I am NOT trying to cross-compile.
I'm using go1.6.windows-386 to compile the 32bit version and when I try to compile with GOARCH=amd64 I use go1.6.windows-amd64.

I used TDM-GCC as linux like compile tools.
The error isn't indicative. it just says

c:/WpdPack/Lib/x64/wpcap.lib: error adding symbols: File in wrong format collect2.exe: error ld returned 1 exit status

Did anyone manage to build this, if it's even possible?

Jan Zerebecki
  • 825
  • 7
  • 18
J. Dow
  • 351
  • 3
  • 5
  • I don't quite parse the question: the only case when you need to specify `GOARCH` is when you're cross-compiling; otherwise `GOARCH` is set by the `go` tools all by themselves (you can verify it by running `go env`). So, do I understand correctly that you 1) have 64-bit Windows; 2) have two Go packages -- i386 and x64 -- installed on it side-by-side; 3) you also have two TDM-GCC packages compiled for different architectures installed side-by-side? – kostix Jun 27 '16 at 11:06
  • If yes, try then mere running of `go build` / `go install` from a shell should invoke the x64 build of Go. Try running those commands with the `-x` command-line option to see what they invoke. – kostix Jun 27 '16 at 11:11
  • @kostix Ok to simpilfy: 1. I have a 64bit windows. 2. I installed 32bit GO 3. I compiled and run my program (needless to say it used cgo to build the gopacket module). 4. I uninstalled the 32bit GO. 5. I installed 64bit GO. 6. I tried to compile my program and failed with the given error. – J. Dow Jun 27 '16 at 12:14
  • So, is your TDD-GCC install x64 as well? – kostix Jun 27 '16 at 12:24
  • @kostix Yes. I have both TDM-GCC x64 and x86. When trying to compile x64 I changed the PATH env variable to the x64 TDM-GCC folder.. – J. Dow Jun 27 '16 at 13:03
  • Try using the `CC_FOR_TARGET` env variable to specify the correct compiler for the target arch. – JimB Jun 27 '16 at 13:59
  • I am trying to compile winpcap example code with TDM-GCC. of course, 32bit compiles easily. 64bit generates the same error. I'm quite sure the problem is the static libraries missing from the /lib/x64 folder. you can try to compile one of the example in the winpcap developers packet [here](https://www.winpcap.org/devel.htm) compile commands for the "basic dump" example: ```gcc -g -O -I ../../include -c -o basic_dump.o basic_dump.c``` ```gcc -g -O -I ../../include -o basic_dump.exe basic_dump.o -L ../../lib/x64 -lwpcap``` – J. Dow Jun 27 '16 at 14:13
  • The guy [here](http://stackoverflow.com/questions/24703765/winpcap-developer-usage-with-cygwin-c-netbeans-ide) mentioned the missing .a files. maybe I can generate the .a files like [this](https://sourceforge.net/p/mingw-w64/discussion/723797/thread/530727b3/)? – J. Dow Jun 27 '16 at 14:14

3 Answers3

28

OK so I have figured it out.
In order to compile gopacket 64bit on windows you need to do the following:

  1. Install go_amd64 (add go binaries to your PATH)
  2. Install TDM GCC x64 (add TDM-GCC binaries to your PATH)
  3. Also add TDM-GCC\x86_64-w64-mingw32\bin to your PATH
  4. Install Winpcap
  5. Download Winpcap developer's pack and extract it to C:\

Now the point is that there are missing linux static libraries files
(libwpcap.a and libpacket.a) from lib/x64 folder. I don't know why they weren't
included in the developers pack but anyway that's how we can generate them:

  1. find wpcap.dll and packet.dll in your PC (typically in c:\windows\system32
  2. copy them to some other temp folder or else you'll have to supply Admin privs to the following commands
  3. run gendef on those files gendef wpcap.dll and gendef packet.dll (obtainable with MinGW Installation Manager, package mingw32-gendef)
  4. this will generate .def files
  5. Now we'll generate the static libraries files:
  6. run dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib libwpcap.a --input-def wpcap.def
  7. and dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib libpacket.a --input-def packet.def
  8. Now just copy both libwpcap.a and libpacket.a to c:\WpdPack\Lib\x64

That's it.
Now gopacket should compile with no problems.

Rubber Duck
  • 2,997
  • 2
  • 20
  • 25
J. Dow
  • 351
  • 3
  • 5
0

Thank you so much for the solution, it saved me a lot of time!

Just wanted to add that you can do the same with Npcap, modify the gopacket source code to point to Npcap and it will work too.

In case you don't know Npcap:

0

I installed Npcap on Windows in "Wpcap API Compatibility Mode" and gopacket now works fine.

openwonk
  • 14,023
  • 7
  • 43
  • 39