从Go 1.6开始,这段代码:
argc := len(*argv)
c_argc := C.int(argc)
c_argv := make([]*C.char, argc)
for index, value := range *argv {
c_argv[index] = C.CString(value)
defer C.free(unsafe.Pointer(c_argv[index]))
}
err := C.MPI_Init(&c_argc, (***C.char)(unsafe.Pointer(&c_argv)))
不再工作,但runtime error: cgo argument has Go pointer to Go pointer
失败。我已经阅读过使用malloc()在C中分配数组,然后复制所有内容,但这很难做到(因为argv元素具有不同的长度而且非常无用
答案 0 :(得分:5)
这是一个最小的例子,我的C技能相当生疏,所以可能有更好的方法:
package main
/*
#include <stdlib.h>
#include <stdio.h>
static void* allocArgv(int argc) {
return malloc(sizeof(char *) * argc);
}
static void printArgs(int argc, char** argv) {
int i;
for (i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
}
*/
import "C"
import (
"os"
"unsafe"
)
func main() {
argv := os.Args
argc := C.int(len(argv))
c_argv := (*[0xfff]*C.char)(C.allocArgv(argc))
defer C.free(unsafe.Pointer(c_argv))
for i, arg := range argv {
c_argv[i] = C.CString(arg)
defer C.free(unsafe.Pointer(c_argv[i]))
}
C.printArgs(argc, unsafe.Pointer(c_argv))
}
答案 1 :(得分:2)
无法在堆栈溢出时添加注释不够点,但是当我使用go 1.9运行时我必须将最后一行转换为(** C.char)
C.printArgs(argc, (**C.char)(unsafe.Pointer(c_argv)))