gpt4 book ai didi

c - 接口(interface) Go 与 C 库

转载 作者:IT老高 更新时间:2023-10-28 13:02:21 28 4
gpt4 key购买 nike

如何将 Go 程序与 C 库接口(interface)?

我一直在浏览 Go 的源代码,但还是没有弄明白。如果有人已经这样做了,你能分享一下吗?

更新:感谢@fserb,我发布了一些来自 Go 资源的文档:

Cgo enables the creation of Go packages that call C code.

Usage: cgo [compiler options] file.go

The compiler options are passed through uninterpreted when invoking gcc to compile the C parts of the package.

The input file.go is a syntactically valid Go source file that imports the pseudo-package "C" and then refers to types such as C.size_t, variables such as C.stdout, or functions such as C.putchar.

If the import of "C" is immediately preceded by a comment, that comment is used as a header when compiling the C parts of the package. For example:

// #include <stdio.h>
// #include <errno.h>
import "C"

Cgo transforms the input file into four output files: two Go source files, a C file for 6c (or 8c or 5c), and a C file for gcc.

The standard package makefile rules in Make.pkg automate the process of using cgo. See $GOROOT/misc/cgo/stdio and $GOROOT/misc/cgo/gmp for examples.

Cgo does not yet work with gccgo.

最佳答案

检查 cgo .此外,请查看 Go 源代码上的 misc/cgo/gmp 以获取有关如何在 Go 中包装 C 库的示例代码。

关于c - 接口(interface) Go 与 C 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1760468/

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