安装gccgo以测试Protocol Buffers 3和Go



我正在尝试安装gccgo,以便用Golang测试协议缓冲区3。。。我不得不承认,我在休假8年后又回到了dev(我不是母语人士(,所以,谢谢你的宽容。谢谢:(

因此,经过几次阅读,我决定从本回购的自述开始:https://github.com/golang/protobuf

第一个要点:已检查

协议缓冲区的最后一个版本安装在我的Mac上(protobuf-cpp-3.11.4.tar.gz据我所知(https://github.com/protocolbuffers/protobuf/releases/tag/v3.11.4

$ ls $GOBIN
dlv*           gocode*        godef*         gopkgs*        protoc-gen-go*
go-outline*    gocode-gomod*  golint*        goreturns*

第二个要点:我花了几个小时。。。没有成功:/

当然,安装Go编译器和工具https://golang.org/看见https://golang.org/doc/install有关详细信息,或者,如果您正在使用gccgo,请按照https://golang.org/doc/install/gccgo

我的理解是,我需要安装gccgo,它是gcc编译器的一个分支。然后我读到gccgo实际上只是gcc编译器的自定义构建,配置有--enable-languages=c,c++,go选项(srchttps://golang.org/doc/install/gccgo(。。。那么,为什么回购协议上有一个特殊的分支机构,它在哪里呢?(https://gcc.gnu.org/git.html)I

我只是放弃了尝试从git存储库下载gccgo分支并找到svn repo:

$ svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo`
gccgo$ ./configure --enable-languages=c,c++,go
...
configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  Source code for these libraries can be found at
their respective hosting sites as well as at
<https://gcc.gnu.org/pub/gcc/infrastructure/>.  See also
<http://gcc.gnu.org/install/prerequisites.html> for additional info.  If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files.  They may be located in separate packages.

所以,我从下载了gmp-6.2.0.tar.lzhttps://gmplib.org/这导致我在解开存档之前安装lzip

$ brew install lzip
$ lunzip gmp-6.2.0.tar.lz
$ tar - xvzf gmp-6.2.0.tar
$ cd gmp-6.2.0
gmp-6.2.0$ ./configure
gmp-6.2.0$ make
gmp-6.2.0$ make install
gmp-6.2.0$ make check ( a few warnings but every test have been passed successfully )

然后,安装mpfr-3.1.6.tar.gz

$ tar -xvzf mpfr-3.1.6.tar.gz
$ cd mpfr-3.1.6
mpfr-3.1.6$ ./configure
mpfr-3.1.6$ ./make
mpfr-3.1.6$ ./make install

然后重试

gccgo$ ./configure --enable-languages=c,c++,go
...
The following requested languages could not be built: go
Supported languages are: c,brig,c,c++,d,fortran,lto,objc,obj-c++

最后

我不确定他们在最后一步中谈论的目录。。。

使用"make Go"在该目录中构建Go示例。这将创建以下可执行文件当前目录中的文件:添加人员列表人员转到

makegcc一起引入了一个单独的"规则"文件,该文件描述如何从源代码到完成的程序,解释该文件,找出需要编译的内容,并调用gcc。(来源https://stackoverflow.com/a/768379/1216281(。因此,如果gcc-it没有正确编译,它就无法工作。

protocolbuffer$ ls
add_person.go        add_person_test.go   addressbook.proto    list_people_test.go
add_person.go.txt    addressbook.pb.go    list_people.go
protocolbuffer$ make go
make: *** No rule to make target `go'.  Stop.

额外技术信息(如果需要(:

~$ echo $GOPATH
/Users/me/Dev/Go/golib:/Users/me/Dev/Go/code
$GOBIN is /Users/me/Dev/Go/golib/bin
$ echo $GOBIN
/Users/me/Dev/Go/golib/bin

为了在go中编译protobufs,您需要有go compiler和以下包

go get github.com/golang/protobuf
go get github.com/golang/protobuf/proto

如果您的GOPATH包含在PATH环境中,您应该能够从终端执行protoc二进制文件。

让我们试一个简单的例子。首先定义一个protobuf模式,它表示某个对象。它看起来有点像

syntax="proto3";
package main;
message Person {
string name = 1;
int32 age = 2;
}

person.proto

下一步是使用protoc将其编译为go源代码

protoc --go_out=. *.proto

它将在文件person.pb.go中生成一个go源代码文件,代表您的原型message

让我们看看如何在main.go中使用它

package main
import (
"fmt"
"os"
"github.com/golang/protobuf/proto"
)
func main() {
p := &Person{
Name: "John Doe",
Age:  30,
}
data, err := proto.Marshal(p)
if err != nil {
fmt.Printf("marshaling error: %v", err)
os.Exit(1)
}
fmt.Printf("our raw protobuf object looks like: %+vnits type is %Tn", data, data)
// let's unmarshal it (from byte array) to an object we can use as Person
newP := &Person{}
err = proto.Unmarshal(data, newP)
if err != nil {
fmt.Printf("unmarshaling error: %v", err)
os.Exit(1)
}
// now we can use our unmarshaled data as a struct
fmt.Printf("newP name: %vnnewP age: %vnnewP type: %Tn", newP.GetName(), newP.GetAge(), newP)
}

让我们运行

→ go run .
our raw protobuf object looks like: [10 8 74 111 104 110 32 68 111 101 16 30]
its type is []uint8
newP name:  John Doe
newP age:  30
newP type: *main.Person

您可以在person.pb.go中查看自动生成的源代码。希望这能有所帮助。

最新更新