go - cgo - 在 MacOSX 中找不到 'runtime.h' 文件

标签 go cgo

我正在尝试执行提到的 cgo 程序 here

    package main

    /*
    #include "runtime.h"

    int goId() {
            return g->goid;
    }
    */
    import "C"
    import "fmt"

    func main() {
     x := C.goId()
     fmt.Printf("Id - %d", x)
    }

在运行上述程序时出现以下错误:-

jab-MacBook-Pro-4:src debraj$ go build gid.go
# command-line-arguments
./gid.go:4:10: fatal error: 'runtime.h' file not found
#include "runtime.h"
         ^
1 error generated.

如果我将标题更改为如下所示:-

#include <objc/runtime.h>

然后它给我以下错误:-

jab-MacBook-Pro-4:src debraj$ go build gid.go
# command-line-arguments
./gid.go:7:9: error: use of undeclared identifier 'g'
        return g->goid;
               ^
1 error generated.

环境

  • MacOSX - 10.11.6
  • 开始 - 1.7.3

谁能告诉我如何在 MacOSX 中运行上述程序?

最佳答案

golang-nuts 中所述从 Go 1.5 开始这是不可能的

That's not a cgo program there, but a Go program, which uses the gc compiler's affinity to compile C programs into the resulting binary.

But since Go 1.5.0, that C backend is removed (the last version is 1.4.2), so that side effect has vanished.

关于go - cgo - 在 MacOSX 中找不到 'runtime.h' 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163361/

相关文章:

go - 将C结构从主要golang代码传递到不同的golang程序包中

go - 在不知 Prop 体类型的情况下解码 gob 输出

go - 这是一种特殊形式的 Go 类型断言吗?

戈朗 : cgo extern is not working

go - 我是否必须释放使用 Cgo 创建的结构?

CGo 将 go 字符串转换为 *C.uchar

go - 为什么 Go 的 bufio 在后台使用 panic?

go - 检查结构的任何字段是否为零

go - go 中包含库的类型不匹配

python - 如何从go返回错误字符串到python