cgo

Cgo: sorry, unimplemented: 64-bit mode not compiled in

心不动则不痛 提交于 2021-02-18 10:25:06
问题 I'm currently trying to add some C code to my Go project. nothing fancy /* #include <stdio.h> void test() { printf("hooola") } */ import ( "C" ) func MessageBox() { C.test() } However this will return cc1.exe: sorry, unimplemented: 64-bit mode not compiled in I checked my g++ and gcc compilers and everything seems fine, g++ -v returns this C:\Users\ragga>g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw-w64/x86_64-6.2.0-posix-seh-rt_v5-rev1/mingw64/bin/.

How to get string from argument(char*) passed to C function?

不羁岁月 提交于 2021-02-10 12:58:06
问题 It's hard for me to describe the problem precisely. Let me show the code below: I have a function in C : int foo(char *deviceID){ char a[]="abc"; deviceID=a; return 1; } Obviously I pass the char*deviceID arguments to be changed in this function, just ignore the return integer, what I want is to get the value of deviceID ; In Go : func getID() string{ var b []byte C.foo((*C.char)(unsafe.Pointer(&b))) return string(b) } But it seems I got nothing. Can anyone help me find out the problem? 回答1:

Use Go slice in C

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 10:36:31
问题 I'm trying to build a go shared library with a function that returns a slice. How can I use the slice from C code ? package main import "C" type T struct { A C.int B *C.char } //export Test func Test() []T { arr := make([]T, 0) arr = append(arr, T{C.int(1), C.CString("a")}) arr = append(arr, T{C.int(2), C.CString("abc")}) return arr } func main() {} go build -o lib.so -buildmode=c-shared main.go I now have a lib.so and a lib.h What would be the C code to print the values of the array ?

From []byte to char*

一世执手 提交于 2021-02-06 10:16:50
问题 I want to wrap a C function that takes a char* pointing to (the first element of) a non-empty buffer of bytes. I'm trying to wrap that in a Go function using CGo so that I can pass it a []byte , but I don't know how to do the conversion. A simplified version of the C function's signature is void foo(char const *buf, size_t n); I tried passing a pointer to the first byte in the slice with C.foo(&b[0], C.size_t(n)) That doesn't compile, though: cannot use &b[0] (type *byte) as type *_Ctype_char

Calling C function that requires sockaddr

穿精又带淫゛_ 提交于 2021-02-04 16:27:09
问题 I have a C function that takes a sockaddr pointer. I've created a syscall.RawSockaddrInet4 object, but I cannot figure out how to cast the address of the object to a C.sockaddr pointer. I've tried this, (*syscall.RawSockaddr)(unsafe.Pointer(&sa)), but I get an error: cannot use (*syscall.RawSockaddr)(unsafe.Pointer(&sa)) (type *syscall.RawSockaddr) as type *C.struct_sockaddr in argument to _Cfunc_lib_connect Any ideas would be much appreciated! int lib_connect( int sock, enum lib_sock_type

multiple definition when using cgo

此生再无相见时 提交于 2021-01-29 09:24:12
问题 package main /* int add(int a, int b) { return a + b; } */ import "C" import "fmt" func main() {} func Test1() { fmt.Println(C.add(1, 3)) } //export Test2 func Test2() { } Compile the programe: dingrui@dingrui-PC:~/Projects/gotest/array$ go build -o libtest.so -buildmode=c-shared main.go # command-line-arguments /tmp/go-build043762604/b001/_x002.o: In function `add': ./main.go:5: multiple definition of `add' /tmp/go-build043762604/b001/_x001.o:/tmp/go-build/main.go:5: first defined here

Unable to Call unsafe.Pointer(Pointed to C Function) as Function

偶尔善良 提交于 2021-01-28 07:55:13
问题 I'm trying to call C function with same signature they take 2 int arguments. and this error cannot call non-function f (type unsafe.Pointer) appear during compile. package main /* int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } */ import "C" import ( "fmt" "unsafe" ) func main() { a := C.int(1) b := C.int(2) fx := make([]unsafe.Pointer, 2) fx[0] = C.add fx[1] = C.sub for _, f := range fx { fmt.Printf("Result: %d\n", f(a, b)) } } Now I'm working around with wrap C

Override an external package's cgo compiler and linker flags?

感情迁移 提交于 2020-12-30 06:34:08
问题 Let's say I want to use some awesome go package. I can include it by: import "github.com/really-awesome/project/foobar" And inside that project's foobar.go file, it defines some cgo instructions like: #cgo windows CFLAGS: -I C:/some-path/Include #cgo windows LDFLAGS: -L C:/some-path/Lib -lfoobar But if I have that foobar C dependency installed somewhere else, I would really need those lines to say: #cgo windows CFLAGS: -I C:/different-path/Include #cgo windows LDFLAGS: -L C:/different-path

passing function pointer to the C code using cgo

穿精又带淫゛_ 提交于 2020-12-29 04:13:50
问题 Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* extern void go_callback_int(void* foo, int p1); // normally you will have to define function or variables // in another separate C file to avoid the multiple definition // errors, however, using "static inline" is a nice workaround // for simple functions like this

How do I convert a Go array of strings to a C array of strings?

谁都会走 提交于 2020-12-29 04:11:38
问题 I am using cgo in a project, and I want to export a function for use. Here's an example of what I want to achieve: package csplit import ( "C" "strings" ) //export Split /* The Split function takes two C strings, the second of which represents a substring to split on, and returns an array of strings. Example: Split("1,2", ",") // gives ["1", "2"] */ func Split(original *C.char, split *C.char) []*C.char { goResult := strings.Split(C.GoString(original), C.GoString(split)) cResult := make([]*C