英文:
Go build shared-c is not outputting a header file
问题
我正在尝试在Go语言中构建一个共享的C库,并且输出没有创建头文件(.h)。
test.go:
package main
import "C"
import "fmt"
func ExportedFun(s string) {
fmt.Printf("C gave us %s string", s)
}
func main() {}
我运行的命令是:
go build -buildmode=c-shared -o test.so test.go
我得到了.so
文件,但没有头文件。我是否漏掉了什么?
英文:
I am trying to test build a shared C lib in GoLang, and the output does not create a header file (.h)
test.go:
package main
import "C"
import "fmt"
func ExportedFun(s string) {
fmt.Printf("C gave us %s string", s)
}
func main() {}
and the command I run is:
go build -buildmode=c-shared -o test.so test.go
I get the .so
file but no header file. Is there something I am missing?
答案1
得分: 22
从go命令文档中可以找到以下内容:
只有使用cgo的导出注释导出的函数才能被调用。
通过cgo导出函数的语法可以在cgo文档中找到:
可以通过以下方式将Go函数导出供C代码使用:
//export MyFunction func MyFunction(arg1, arg2 int, arg3 string) int64 {...} //export MyFunction2 func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
将函数标记为导出函数将生成头文件。
英文:
From the go command documentation:
> The only callable symbols will be those functions exported using a cgo
> //export comment.
Th syntax for exporting a function via cgo can be found in the cgo documentation
> Go functions can be exported for use by C code in the following way:
>
> //export MyFunction
> func MyFunction(arg1, arg2 int, arg3 string) int64 {...}
>
> //export MyFunction2
> func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
Marking your function as exported will generate the header.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论