观察:
$ cat /tmp/plugin.go
package main
import "fmt"
var V int
func F() { fmt.Printf("Hello, number %d\n", V) }
$ go build -buildmode=plugin -o /tmp/plugin.so /tmp/plugin.go
# runtime/cgo
cgo-builtin-prolog:1:57: fatal error: stddef.h: No such file or directory
compilation terminated.
为什么?
这是在Ubuntu 17.04下,我已经安装了build-essentials:
$ go version
go version go1.9 linux/amd64
$ uname -r
4.10.0-37-generic
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.04
Release: 17.04
Codename: zesty
$ apt-cache policy build-essential
build-essential:
Installed: 12.1ubuntu2
Candidate: 12.1ubuntu2
请不要“Compilation error: "stddef.h: No such file or directory"”不是答案,因为我的gcc-core软件包和gcc-g ++ 是相同的版本 - 这里是我的gcc相关包:
gcc_4:6.3.0-2ubuntu1
gcc-6_6.3.0-12ubuntu2
gcc-6-base:amd64_6.3.0-12ubuntu2
libgcc-6-dev:amd64_6.3.0-12ubuntu2
libgcc1:amd64_1:6.3.0-12ubuntu2
更新:
感谢@peterSO,似乎是我的gcc自己的问题:
$ cat /tmp/foo.c
#include <stdio.h>
$ gcc /tmp/foo.c
In file included from /tmp/foo.c:1:0:
/usr/include/stdio.h:33:21: fatal error: stddef.h: No such file or directory
# include <stddef.h>
在搜索解决方案时,我发现有人建议重新安装gcc,所以我和libc6-dev
一起做了:
apt-get --reinstall install libgcc-6-dev gcc-6 gcc-6-base:amd64 libc6-dev:amd64
现在我的简单.c文件现在编译好了,但是我遇到了cgo
的新问题:
$ go build -buildmode=plugin -o /tmp/plugin.so /tmp/plugin.go
# runtime/cgo
In file included from /usr/include/errno.h:35:0,
from cgo-gcc-prolog:21:
/usr/include/x86_64-linux-gnu/bits/errno.h:24:26: fatal error: linux/errno.h: No such file or directory
# include <linux/errno.h>
^
compilation terminated.
我知道这可能是Ubuntu / gcc特定的问题(参考:Why is stddef.h not in /usr/include?而我的stddef.h
在/usr/src/linux-headers-4.10.0-37/include/linux/stddef.h
下),但是这里的任何人都知道Go和gcc足够了解如何修复它,以便Go插件可以正确编译?
THX!