cgo – 在MacOSX中找不到 ‘runtime.h’ 文件

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

cgo - 'runtime.h' file not found in MacOSX

问题

我正在尝试执行如下所述的cgo程序:

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
  • Go - 1.7.3

有人可以告诉我如何在MacOSX上运行上述程序吗?

英文:

I am trying to execute the cgo program as mentioned here

    package main

    /*
    #include &quot;runtime.h&quot;
    
    int goId() {
            return g-&gt;goid;
    }
    */
    import &quot;C&quot;
    import &quot;fmt&quot;
    
    func main() {
     x := C.goId()
     fmt.Printf(&quot;Id - %d&quot;, x)
    }

On running the above program I am getting the below error:-

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

If I change the header to be like below:-

#include &lt;objc/runtime.h&gt;

then it is giving me the below error:-

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

Environment

  • MacOSX - 10.11.6
  • Go - 1.7.3

Can someone let me know how can I run the above program in MacOSX?

答案1

得分: 0

根据golang-nuts中的讨论,截至Go 1.5版本,这是不可能的。

那不是一个cgo程序,而是一个使用gc编译器将C程序编译到生成的二进制文件中的Go程序。

但是自从Go 1.5.0版本开始,该C后端已被移除(最后一个版本是1.4.2),因此该副作用已经消失。

英文:

As discussed in golang-nuts this is not possible as of 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.

huangapple
  • 本文由 发表于 2016年12月15日 19:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/41163361.html
匿名

发表评论

匿名网友

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

确定