Answer the question
In order to leave comments, you need to log in
Calling C functions with data from a Go package, is it possible?
Good afternoon.
Looked at the documentation:
var arr [10]int
C.MyFunc(arr, 10)
void MyFunc(int *arr, int len);
package libs
func MySum(arr [4]int) int {
my_arr := unsafe.Pointer(&arr[0])
my_len := C.int(len(arr))
res := C.Sum1((*C.int)(my_arr), my_len)
return int(res)
}
int Sum1(int *arr, int len)
{
std::cout << "---------- inside Sum1 (C++) ------------\n";
int sum = 0;
std::cout << "len=" << len << std::endl;
for (int i=0; i<len; ++i)
{
std::cout << "arr[" << i << "]=" << arr[i] << std::endl;
sum += arr[i];
}
std::cout << "sum=" << sum << std::endl;
std::cout << " ---------- Sum1 (C++) end ------------\n";
return sum;
}
arr := [4]int{1, 2, 3, 4}
fmt.Println(libs.MySum(arr))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question