cgo

compile gopacket on windows 64bit

你。 提交于 2019-12-03 14:57:22
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

Conditional compilation in Go

冷暖自知 提交于 2019-12-03 14:49:27
I'm trying to write a Go wrapper using CGo for ENet . When I tried to compile my wrapper on a Mac the library was older and had a slightly different interface. 99% of the code is the same just a few C calls need to change. What is the best practice for dealing with a problem like this in Go? Is there some way to do conditional compilation or conditional imports? Go does not have conditional compilation or conditional imports. Handle the type differences in C code. Are the [Go] authors opposed to preprocessing? Separate out the platform-specific stuff into a separate file, e.g. stuff.go Now

How to use a relative path for LDFLAGS in golang

为君一笑 提交于 2019-12-03 14:38:00
I am trying to build a golang program which uses a static lib (.a file) the directory struct for my project as below └─testserver ├─bin ├─pkg └─src ├─logging └─testserver ├─libtest.a └─test.go the flags for cgo in test.go as below // #cgo LDFLAGS: -L /home/test/testserver/src/testserver -ltest // #include "test.h" import "C" when I am using absolute path for LDFLAGS -L, it works fines, but when I change the path to a relative path, eg // #cgo LDFLAGS: -L ./testserver -ltest and then run the command go install testserver it returns an error to me, and says "cannot find -ltest" my question is

Compile cgo lib on Cygwin64: “ld: cannot find -lmingw32”

时光怂恿深爱的人放手 提交于 2019-12-03 12:34:43
问题 I'm trying to use a cgo library on Windows, namely github.com/mattn/go-sqlite3 I use Cygwin64 and installed with all "Development" packages, so gcc is availabe. But running go get github.com/mattn/go-sqlite3 results in: /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lmingwex /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lmingw32 If I search for "mingwex" and "mingw32" in the Cygwin installer, I get no results. Am I

Convert Go []byte to a C *char

时间秒杀一切 提交于 2019-12-03 09:41:03
问题 I have a byte.Buffer that I pack with data using the binary.Write() function. I then need to send this byte array to a C function. Using Go 1.6 I have not been successful at figuring this out. buf := new(bytes.Buffer) //create my buffer .... binary.Write(buf, binary.LittleEndian, data) //write my data to buffer here addr := (*C.uchar)(unsafe.Pointer(&buf.Bytes()[0])) //convert buffers byte array to a C array rc := C.the_function(addr, C.int(buf.Len())) //Fails here It fails on the line

Compile cgo lib on Cygwin64: “ld: cannot find -lmingw32”

随声附和 提交于 2019-12-03 03:52:25
I'm trying to use a cgo library on Windows, namely github.com/mattn/go-sqlite3 I use Cygwin64 and installed with all "Development" packages, so gcc is availabe. But running go get github.com/mattn/go-sqlite3 results in: /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lmingwex /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lmingw32 If I search for "mingwex" and "mingw32" in the Cygwin installer, I get no results. Am I looking for the wrong names or are they not available on 64 bit systems? Or is there a better way to

Convert Go []byte to a C *char

≯℡__Kan透↙ 提交于 2019-12-03 03:49:10
I have a byte.Buffer that I pack with data using the binary.Write() function. I then need to send this byte array to a C function. Using Go 1.6 I have not been successful at figuring this out. buf := new(bytes.Buffer) //create my buffer .... binary.Write(buf, binary.LittleEndian, data) //write my data to buffer here addr := (*C.uchar)(unsafe.Pointer(&buf.Bytes()[0])) //convert buffers byte array to a C array rc := C.the_function(addr, C.int(buf.Len())) //Fails here It fails on the line calling the C function saying: panic: runtime error: cgo argument has Go pointer to Go pointer The C function

Export function that returns array of doubles

风流意气都作罢 提交于 2019-12-02 10:57:48
问题 In Golang how to export the function that returns array of doubles. The way it was possible before seems to return "runtime error: cgo result has Go pointer" now: //export Init func Init(filename string) (C.int, unsafe.Pointer) { var doubles [10]float64 doubles[3] = 1.5 return 10, unsafe.Pointer(&doubles[0]) } 回答1: In order to safely store a pointer in C, the data it points to must be allocated in C. //export Init func Init(f string) (C.size_t, *C.double) { size := 10 // allocate the *C

casting a cgo array into a slice

本小妞迷上赌 提交于 2019-12-02 09:46:30
At the moment I do this for casting a CGO array of doubles into a slice of float64: doubleSlc := [6]C.double{} // Fill doubleSlc floatSlc := []float64{float64(doubleSlc[0]), float64(doubleSlc[1]), float64(doubleSlc[2]), float64(doubleSlc[3]), float64(doubleSlc[4]), float64(doubleSlc[5])} Is there a less cumbersome way of doing the same thing? I suppose this can also be seen as a general way of casting between slices/arrays of different types in Go. You have the normal and safe way of doing this: c := [6]C.double{ 1, 2, 3, 4, 5, 6 } fs := make([]float64, len(c)) for i := range c { fs[i] =

Export function that returns array of doubles

烈酒焚心 提交于 2019-12-02 07:03:36
In Golang how to export the function that returns array of doubles. The way it was possible before seems to return "runtime error: cgo result has Go pointer" now: //export Init func Init(filename string) (C.int, unsafe.Pointer) { var doubles [10]float64 doubles[3] = 1.5 return 10, unsafe.Pointer(&doubles[0]) } In order to safely store a pointer in C, the data it points to must be allocated in C. //export Init func Init(f string) (C.size_t, *C.double) { size := 10 // allocate the *C.double array p := C.malloc(C.size_t(size) * C.size_t(unsafe.Sizeof(C.double(0)))) // convert the pointer to a go