gpt4 book ai didi

Go build shared-c 不输出头文件

转载 作者:IT王子 更新时间:2023-10-29 01:49:40 24 4
gpt4 key购买 nike

我正在尝试在 GoLang 中测试构建一个共享的 C 库,但输出没有创建头文件 (.h)

测试.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 文件,但没有头文件。有什么我想念的吗?

最佳答案

来自go command documentation :

The only callable symbols will be those functions exported using a cgo //export comment.

可以在 cgo documentation 中找到通过 cgo 导出函数的语法。

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) {...}

将您的函数标记为已导出将生成 header 。

关于Go build shared-c 不输出头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34933178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com