英文:
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 "runtime.h"
int goId() {
return g->goid;
}
*/
import "C"
import "fmt"
func main() {
x := C.goId()
fmt.Printf("Id - %d", 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: 'runtime.h' file not found
#include "runtime.h"
^
1 error generated.
If I change the header to be like below:-
#include <objc/runtime.h>
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 'g'
return g->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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论