[英]How do I call a Go function from C code embedded in cgo
This is super basic, apologies for the rudimentary ask.这是超级基本的,为基本的问题道歉。 I've got Go code, which calls a C function (embedded in the same Go package via cgo), which should call back another Go function.
我有 Go 代码,它调用一个 C 函数(通过 cgo 嵌入在同一个 Go 包中),它应该回调另一个 Go 函数。 Calling the C code works, and the code compiles, but upon linking, the C linker doesn't find the Go function.
调用 C 代码有效,代码编译,但在链接时,C 链接器找不到 Go 函数。
cpoc % go build .
# cpoc
Undefined symbols for architecture x86_64:
"_goLogger", referenced from:
_cLogger in _x003.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
poc %
cpoc.go cpoc.go
package main
// #cgo pkg-config: vips
// #include "cpoc.h"
import "C"
import (
"fmt"
)
// export goLogger
func goLogger() {
fmt.Println("goLogger")
}
func main() {
C.cLogger()
}
cpoc.h cpoc.h
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
extern void goLogger(void);
void cLogger(void);
cpoc.c cpoc.c
#include "cpoc.h"
void cLogger(void)
{
printf("cLogger\n");
goLogger();
}
The space between // and export caused the function not be exported. // 和 export 之间的空格导致函数不能被导出。
Answer based on @kostix comment and it was correct.基于@kostix 评论的答案是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.