Go build shared-c没有输出头文件。

huangapple go评论122阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年1月22日 03:59:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/34933178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定